Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 947 Bytes

jq.md

File metadata and controls

59 lines (40 loc) · 947 Bytes

jq

jq . filename.json
cat filename.json | jq .

access some value

# the value at key "foo"
echo '{"foo": "bar"}' | jq '.foo'

# first item of a list
echo "[1, 2, 3, 4]" | jq '.[0]'

slice & dice

jq '.[2:4]'
jq '.[:3]'
jq '.[-2:]'

Get 'name' for each object in list 'accounts'

jq '.accounts[] | .name'

Objects

# extract items from an object
echo '{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}' | jq '{a: .foo, b: .baz}'
# → {"a":"FOO", "b":"BAZ"}

Filter attributes

# blacklist 'foo'
echo '{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}' | jq '. |= del(.foo)'

# whitelist 'bar', 'baz'
echo '{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}' | jq '. |= {bar, baz}'

String interpolation

jq -r '.users[] | "\(.first) \(.last)"' <<EOF
{ "users": [ { "first": "Stevie", "last": "Wonder" },
             { "first": "Michael", "last": "Jackson" }]}
EOF