jq
is a powerful tool to work with JSON from the command-line.
The tool has a lot of functions that makes our live easier.
With jq
we can use expressions in strings that will be evaluated and inserted into the string value.
This is called string interpolation.
The expression is enclosed by parentheses and the first parenthesis is prefixed with a backslash: \(<expression>)
.
The expression can be any valid jq
expression and the result of the expression will be inserted into the string.
In the following example we use string interpolation to print a greeting message and we use a simple property reference, but also a bit more complicated expressions:
$ jq --raw-output --null-input '{"prefix": "Hi", "name": "mrhaki", "visitors": 41} | "\(.prefix),\nwelcome \(.name | ascii_upcase)\nYou are visitor \(.visitors + 1)!"' Hi, welcome MRHAKI You are visitor 42!
Written with jq 1.7.