Post

jq - Create json from scratch

How do I create a json structure from scratch using only jq?

The command line option --arg name value

passes a value to the jq program as a predefined variable… so --arg foo 123 will bind $foo to "123" - https://stedolan.github.io/jq/manual/

These named arguments are also available from $ARGS.named. Found via https://stackoverflow.com/a/59769162

The snippet below creates a json structure that you can pipe to a file. It also prints the contents of $ARGS to show what arguments are named and which are positional. jq also support linefeeds, so we can make the json object look pretty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$reponame="awesome-repo"
$branchname="main"

jq -n '
  {
    github_meta: {
      reponame: $reponame,
      branch_name: $branchname
    }, 
    feature: {}, 
    all: $ARGS
  }' \
  --arg reponame "$reponame" \
  --arg branchname "$branchname" \

# output
{
  "github_meta": {
    "reponame": "awesome-repo",
    "branch_name": "main"
  },
  "feature": {},
  "all": {
    "positional": [],
    "named": {
      "reponame": "awesome-repo",
      "branchname": "main"
    }
  }
}
This post is licensed under CC BY 4.0 by the author.