-
Notifications
You must be signed in to change notification settings - Fork 210
Using jc With Different Shells
You will typically use JSON filter utilities like jq
, jello
, jp
, etc. to work with JSON output in Bash.
$ myvar=$(dig www.google.com | jc --dig | jq -r '.[0].answer[0].data')
$ echo $myvar
64.233.185.104
$ myvar=$(jc dig www.google.com | jello -r '_[0].answer[0].data')
$ echo $myvar
64.233.185.104
-
jq
documentation: https://devdocs.io/jq/ -
jello
documentation: https://github.com/kellyjonbrazil/jello -
jp
documentation: https://jmespath.org/tutorial.html
You can use jq
, jello
, jp
, and other JSON filter utilities on the legacy command line and in batch files
C:> dig www.google.com | jc --dig | jq -r ".[0].answer[0].data"
64.233.185.104
C:> jc dig www.google.com | jello -r "_[0].answer[0].data"
64.233.185.104
-
jq
documentation: https://devdocs.io/jq/ -
jello
documentation: https://github.com/kellyjonbrazil/jello -
jp
documentation: https://jmespath.org/tutorial.html
You can use the from-json
builtin to read JSON data into a variable
$ myvar = dig www.google.com | jc --dig | from-json
$ put $myvar[0]['answer'][0]['data']
▶ 64.233.185.104
See https://elv.sh/ref/builtin.html for more info.
In Next Generation Shell, you will typically use the double-backtick "run and parse" syntax with jc
and built-in features for data filtering and other manipulation. Examples:
``jc dig example.com``[0].answer
``jc ifconfig``.the_one({"name": "en0"}).ipv4_addr
``jc ifconfig``.name.filter(/tun/)
``jc ifconfig``.filter({"name": /tun/}).mtu
Notes:
- Non-zero exit code from
jc
will cause an exception to be thrown. - The double-backtick will parse any JSON, not just output of
jc
. -
the_one()
will throw an exception if there is not exactly one matching item. -
.name
and.mtu
result an array as.FIELD_NAME
on an array is defined as mapping each element to the given field.
Next Generation Shell documentation: https://ngs-lang.org/doc/latest/index.html
You can use the from json
builtin to read JSON data into a variable
> myvar = dig www.google.com | jc --dig | from json
> echo $myvar | get 0.answer.0.data
64.233.185.104
See https://www.nushell.sh/book/ for more info.
You can use the json read
builtin to read JSON data into a variable
$ dig www.google.com | jc --dig | json read myvar
$ json write (myvar[0]['answer'][0]['data']) | json read mydata
$ echo $mydata
64.233.185.104
See https://www.oilshell.org/preview/doc/json.html for more info.
You can use the ConvertFrom-Json
utility to parse JSON output from jc
PS C:> myvar = dig www.google.com | jc --dig | ConvertFrom-Json
PS C:> Write-Output $myvar[0].answer[0].data
64.233.185.104
See https://techgenix.com/json-with-powershell/ for more info.