-
Notifications
You must be signed in to change notification settings - Fork 210
Using jc With Different Shells
jc
can be used with most any shell, but some more modern shells have built-in JSON deserialization support.
Along with built-in object filtering capabilities this makes it very easy to assign a specific JSON value to
a variable without the need to use tools like jq
, jello
, jp
, etc.
This article goes over some simple examples of how jc
can be used in modern and traditional 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
$ myvar=$(jc dig www.google.com | jp -u '[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
For more information and examples on using JSON in Bash, see Practical JSON at the Command Line and Practical JSON at the Command Line (using Jello)
You can use the from-json
builtin to read JSON data into a variable
~> var 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#from-json for more info.
Murex includes a cast json
command to convert a string to a JSON data structure. You can also set the object type directly when setting the variable. (e.g. set json myvar
). Then you can use either Index or Element notation to access nested values within the JSON variable.
~ » jc dig www.google.com -> set json myvar
~ » out $myvar[[.0.answer.0.data]]
64.233.185.104
See https://murex.rocks/ 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.
myvar = ``jc dig www.google.com``[0].answer[0].data
echo(myvar)
Returns:
64.233.185.104
Filter Examples:
``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
> let 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
$ var mydata = myvar[0]['answer'][0]['data']
$ echo $mydata
64.233.185.104
See https://www.oilshell.org/release/latest/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.
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
C:\> jc dig www.google.com | jp -u [0].answer[0].data
64.233.185.104
To assign the value to a variable you can use the FOR /F
command.
At The Command Line
C:\> FOR /F "tokens=* USEBACKQ" %i IN (`dig www.google.com ^| jc --dig ^| jq -r .[0].answer[0].data`) DO SET myvar=%i
C:\> ECHO %myvar%
64.233.185.104
In A Batch File
FOR /F "tokens=* USEBACKQ" %%i IN (`dig www.google.com ^| jc --dig ^| jq -r .[0].answer[0].data`) DO SET myvar=%%i
ECHO %myvar%
Returns:
64.233.185.104
-
jq
documentation: https://devdocs.io/jq/ -
jello
documentation: https://github.com/kellyjonbrazil/jello -
jp
documentation: https://jmespath.org/tutorial.html
See https://stackoverflow.com/questions/6359820/how-to-set-commands-output-as-a-variable-in-a-batch-file for more info.