Skip to content

Jq

"jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text."

Examples

Selecting fields - To select a specific field from a JSON object:

echo '{"name": "John", "age": 30}' | jq '.name'

Filtering objects - To filter JSON objects based on a condition:

echo '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 35}]' | jq 'map(select(.age > 30))'

Mapping and Iterating - To iterate over an array and transform its elements:

echo '[1, 2, 3, 4, 5]' | jq 'map(. * 2)'

Combining Filters - To combine filters to perform more complex operations:

echo '{"name": "Alice", "age": 25, "city": "New York"}' | jq 'select(.age > 20) | .city'

Working with Arrays - To get the length of an array:

echo '[1, 2, 3, 4, 5]' | jq 'length'

Formatting Output - To pretty-print JSON:

echo '{"name": "John", "age": 30}' | jq '.'

Converting JSON to CSV - To convert JSON data to CSV format:

echo '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 35}]' | jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv'