So much to love.
Cookbook stuff
String concatenation
Perform concatenation within parenthesis:
jq
Variable interpolation
Use the --arg
cli argument to pass variables through to the filter expression:
jq '(.foo + "..." + .moo)' <<< '{"foo": "fah", "moo":"min"}' > "fah...min"FAV="redbean mochi" cat desserts.json | jq --arg preferredFlavor "$FAV" '.[] | select(.flavor == $preferredFlavor)'
You can also safely build json objects in the cli this way.
String interpolation
Use \(...)
to interpolate query results into strings delimited with quotes:
echo '{"foo": "bar"}' | jq '"foo = \(.foo)"' > "foo = bar"
Outputting
Select keys from json
# use `{}` operator to construct json object echo '{"one": "two", "buckleMy": "shoe", "three": "four"}' | jq '. | {buckleMy: .buckleMy}' > { "buckleMy": "shoe" } # equivalent shorthand, specify key without leading `.` echo '{"one": "two", "buckleMy": "shoe", "three": "four"}' | jq '. | {buckleMy}' > { "buckleMy": "shoe" }