Monday 7 February 2022

jq (JSON Query) Tool Manual

jq (JSON Query) is JSON processing tool.



Installation

 


Help Page


$ jq --help
jq - commandline JSON processor [version 1.6]

Usage:    jq [options] <jq filter> [file...]
    jq [options] --args <jq filter> [strings...]
    jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

    $ echo '{"foo": 0}' | jq .
    {
        "foo": 0
    }

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to it;
  -r               output raw strings, not JSON texts;
  -R               read raw strings, not JSON texts;
  -C               colorize JSON;
  -M               monochrome (don't colorize JSON);
  -S               sort keys of objects on output;
  --tab            use tabs for indentation;
  --arg a v        set variable $a to value <v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.


Echoing Input (Verifying JSON string)


Let's use the following JSON (example taken from https://json.org/example.html):

$ myjson=''\
'{"web-app": {'\
' "servlet": ['\
'   {'\
'     "servlet-name": "cofaxCDS",'\
'     "servlet-class": "org.cofax.cds.CDSServlet",'\
'     "init-param": {'\
'       "configGlossary:installationAt": "Philadelphia, PA",'\
'       "configGlossary:adminEmail": "ksm@pobox.com",'\
'       "dataStoreConnUsageLimit": 100,'\
'       "dataStoreLogLevel": "debug",'\
'       "maxUrlLength": 500}},'\
'   {'\
'     "servlet-name": "cofaxEmail",'\
'     "servlet-class": "org.cofax.cds.EmailServlet",'\
'     "init-param": {'\
'     "mailHost": "mail1",'\
'     "mailHostOverride": "mail2"}},'\
'   {'\
'     "servlet-name": "cofaxAdmin",'\
'     "servlet-class": "org.cofax.cds.AdminServlet"},'\
'   {'\
'     "servlet-name": "fileServlet",'\
'     "servlet-class": "org.cofax.cds.FileServlet"},'\
'   {'\
'     "servlet-name": "cofaxTools",'\
'     "servlet-class": "org.cofax.cms.CofaxToolsServlet",'\
'     "init-param": {'\
'       "templatePath": "toolstemplates/",'\
'       "log": 1,'\
'       "adminGroupID": 4,'\
'       "betaServer": true}}],'\
' "servlet-mapping": {'\
'   "cofaxCDS": "/",'\
'   "cofaxEmail": "/cofaxutil/aemail/*",'\
'   "cofaxAdmin": "/admin/*",'\
'   "fileServlet": "/static/*",'\
'   "cofaxTools": "/tools/*"},'\
' "taglib": {'\
'   "taglib-uri": "cofax.tld",'\
'   "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}'

 
 
To verify that JSON is well-formatted we can use jq . which echoes JSON input to output (as pretty-printed JSON):

$ echo $myjson | jq .
{
  "web-app": {
    "servlet": [
      {
        "servlet-name": "cofaxCDS",
        "servlet-class": "org.cofax.cds.CDSServlet",
        "init-param": {
          "configGlossary:installationAt": "Philadelphia, PA",
          "configGlossary:adminEmail": "ksm@pobox.com",
          "dataStoreConnUsageLimit": 100,
          "dataStoreLogLevel": "debug",
          "maxUrlLength": 500
        }
      },
      {
        "servlet-name": "cofaxEmail",
        "servlet-class": "org.cofax.cds.EmailServlet",
        "init-param": {
          "mailHost": "mail1",
          "mailHostOverride": "mail2"
        }
      },
      {
        "servlet-name": "cofaxAdmin",
        "servlet-class": "org.cofax.cds.AdminServlet"
      },
      {
        "servlet-name": "fileServlet",
        "servlet-class": "org.cofax.cds.FileServlet"
      },
      {
        "servlet-name": "cofaxTools",
        "servlet-class": "org.cofax.cms.CofaxToolsServlet",
        "init-param": {
          "templatePath": "toolstemplates/",
          "log": 1,
          "adminGroupID": 4,
          "betaServer": true
        }
      }
    ],
    "servlet-mapping": {
      "cofaxCDS": "/",
      "cofaxEmail": "/cofaxutil/aemail/*",
      "cofaxAdmin": "/admin/*",
      "fileServlet": "/static/*",
      "cofaxTools": "/tools/*"
    },
    "taglib": {
      "taglib-uri": "cofax.tld",
      "taglib-location": "/WEB-INF/tlds/cofax.tld"
    }
  }
}

If JSON is not formatted well, jq . will output parsing error:

$ myjson=''\
> '['\
> '    ['\
> '        "first"'\
> '    ],'\
> '    ['\
> '        "second"'\
> '    ],'\
> ']'
 
$ echo $myjson | jq .
parse error: Expected another array element at line 1, column 29 

The error here is a comma after the last array element. After removing it the error goes away.


Getting Values

 

To get the value of the key which has a dash (-) in name, we need to wrap the key name in double quotes:

$ echo $myjson | jq '."web-app"'
{
  "servlet": [
    {
      "servlet-name": "cofaxCDS",
...


To get values at the path of keys, key names should be separated with dots:

$ echo $myjson | jq '."web-app".taglib'
{
  "taglib-uri": "cofax.tld",
  "taglib-location": "/WEB-INF/tlds/cofax.tld"
}

To get elements of an array, use [] behind the array key name:

$ echo $myjson | jq '."web-app".servlet[]'
{
  "servlet-name": "cofaxCDS",
  "servlet-class": "org.cofax.cds.CDSServlet",
  "init-param": {
    "configGlossary:installationAt": "Philadelphia, PA",
    "configGlossary:adminEmail": "ksm@pobox.com",
    "dataStoreConnUsageLimit": 100,
    "dataStoreLogLevel": "debug",
    "maxUrlLength": 500
  }
}
{
  "servlet-name": "cofaxEmail",
  "servlet-class": "org.cofax.cds.EmailServlet",
  "init-param": {
    "mailHost": "mail1",
    "mailHostOverride": "mail2"
  }
}
{
  "servlet-name": "cofaxAdmin",
  "servlet-class": "org.cofax.cds.AdminServlet"
}
{
  "servlet-name": "fileServlet",
  "servlet-class": "org.cofax.cds.FileServlet"
}
{
  "servlet-name": "cofaxTools",
  "servlet-class": "org.cofax.cms.CofaxToolsServlet",
  "init-param": {
    "templatePath": "toolstemplates/",
    "log": 1,
    "adminGroupID": 4,
    "betaServer": true
  }
}

 
Let's create JSON with array at its root:
 
$ myjson=''\
'['\
'    ['\
'        "first"'\
'    ],'\
'    ['\
'        "second"'\
'    ]'\
']'

$ echo $myjson | jq .
[
  [
    "first"
  ],
  [
    "second"
  ]
]

To get elements of the root array:

$ echo $myjson | jq .[]
[
  "first"
]
[
  "second"
]

To get elements at index 0 we can pipe jq commands: .[] gives all elements of the root array (and these elements are arrays themselves) and then .[0] gives all first elements in each this sub-array:

$ echo $myjson | jq ".[] | .[0]"
"first"
"second"

---

Resources:


No comments: