curl and jq 8 cheatsheet

curl and jq 8: jq: filter and transform JSON

14 common curl and jq 8 tasks.

Pretty-print JSON

Format JSON with indentation and colors.

curl -s https://api.example.com/users | jq .

Get a field

Extract one field from an object.

jq '.name' user.json

Get a field without quotes

Output raw strings instead of JSON strings, handy in scripts.

jq -r '.name' user.json

Iterate over an array

Output each element of an array.

jq '.users[]' data.json

Get a field from each element

Output the name of every user.

jq -r '.users[].name' data.json

Filter elements

Keep users older than 30.

jq '.users[] | select(.age > 30)' data.json

Count elements

Count the items in an array.

jq '.users | length' data.json

List an object's keys

Output the keys of an object.

jq 'keys' user.json

Output compact JSON

Print each result on one line, which suits line-oriented tools.

jq -c '.users[]' data.json

Build a new object

Create objects with only the fields you want.

jq '.users[] | {id, name}' data.json

Sort by a field

Sort an array of objects by a field.

jq 'sort_by(.age)' users.json

Group by a field

Group array elements that share a value.

jq 'group_by(.role)' users.json

Convert to CSV

Turn arrays of values into CSV rows.

jq -r '.users[] | [.id, .name, .age] | @csv' data.json

Pass a shell variable

Use a shell value inside a jq filter without quoting problems.

jq --arg name "Ada" '.users[] | select(.name == $name)' data.json

Was this page helpful?