Input JSON used for the examples:
# The 5 latest commits of the jqlang repository on GitHub
jqlang_commits=$(curl 'https://api.github.com/repos/jqlang/jq/commits?per_page=5')
Interactive Testing
Using a tool like ijq you can interactively work on JQ expressions.
Building New Objects
The output of one filter can be passed to another, which can commonly be used to build a new json object from an existing one.
echo $jqlang_commits | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
If you want to do this for a whole list of objects it works almost the same. The resulting objects are by default white space separated, which is best for processing in bash.
echo $jqlang_commits | jq '.[] | {message: .commit.message, name: .commit.committer.name}'
To expression can also be wrapped in []
, which will create a proper json list as the output.
echo $jqlang_commits | jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
Wrapping As List
Wrapping multiple json objects into a list is quite easy, just pass them to jq -s
.
cat my-files/*.json | jq -s
Output Configuration
--raw-output / -r
: If the output is a string, don’t wrap it in""
. Useful for further processing in non json tools like shell loops.--sort-keys / -S
: Output the fields of each object with the keys in sorted order.
Read Expression From File
Read the jq expression from a file instead of specifying in the command (Works similar to awk -f
).
jq -f my-expression.jq
jq --from-file my-expression.jq