jq
A lightweight and flexible command-line JSON processor (https://jqlang.github.io/jq/)
Output a JSON file, in pretty-print format
Output all elements from arrays
(or all key-value pairs from objects) in a JSON file:
Use jq to pretty-print JSON
Filter JSON object by extracting a specific field
jq '.fieldName' file.json
Filter JSON array to extract specific element by index
jq '.[<index>]' file.json
Select multiple fields from JSON
jq '{field1: .field1, field2: .field2}' file.json
Use jq to count the number of elements in an array
jq '.arrayName | length' file.json
Apply a function to each element in a JSON array
jq '.arrayName[] | .fieldName' file.json
Parse JSON from stdin
cat file.json | jq '.fieldName'
Use jq to filter JSON objects by condition
jq 'select(.fieldName == "value")' file.json
Modify JSON field value
jq '.fieldName = "newValue"' file.json
Load JSON from URL
curl -s "http://example.com/file.json" | jq '.fieldName'
Combine operations and select elements from different levels
jq '.[] | {id: .id, name: .info.name}' file.json
Query nested JSON data
jq '.outerField.innerField' file.json
Concatenate fields
jq '.field1 + " " + .field2' file.json
Group by a particular field
jq 'group_by(.fieldName)' file.json
Sort JSON objects by field
jq 'sort_by(.fieldName)' file.json
Use jq to find unique values
Print keys and values of a JSON object
jq 'to_entries | .[] | "\(.key): \(.value)"' file.json
Output compact JSON without whitespace
Use jq to delete a field
jq 'del(.fieldName)' file.json
Combine data from two JSON files
jq -s '.[0] + .[1]' file1.json file2.json
Read JSON objects from a file into an array, and output it
(inverse of jq .[]):
Output the first element in a JSON file
Output the value of a given key of the first element in a JSON file
Output the value of a given key of each element in a JSON file
[ { foo: 1 }, { foo: 2 } ]
=> [1, 2]
Extract as stream of values instead of a list
[ { "foo": 1 }, { "foo": 2 } ]
=> 1, 2
Slicing
[ { "foo": 1 }, { "foo": 2 } ]
=> { "foo": 2 }
Dictionary subset shorthand
[ { "a": 1, "b": 2, "c": 3 }, ...]
=> [ { "a": 1, "b": 2 }, ...]
Converting arbitrary data to json
jq -r '(map(keys) | add | unique | sort) as $cols \
| .[] as $row | $cols | map($row[.]) | @csv'
[ { "foo": 1, "bar": 2}, { "foo": 3, "baz": 4}]
=> 2,,1
,4,3
Filter a list of objects
jq 'map(select(.name == "foo"))'
[ { "name": "foo" }, { "name": "bar" } ]
=> [ { "name": "foo" } ]
Add + 1 to all items
[[1], [2]]
=> [1, 2]
Create a range of numbers
Display the type of each item
Sort an array of basic type
[3, 2, 1]
=> [1, 2, 3]
Sort an array of objects
Sort lines of a file
jq --slurp '. | sort | .[]'
Group by a key - opposite to flatten
Minimum value of an array
See also min, max, min_by(path_exp), max_by(path_exp)
Remove duplicates
[1, 1, 2, 1]
=> [1, 2]
Reverse an array
jq in shell scripts
URL Encode something
Thu%2021%20May%202020%2012%3A40%3A40%20PM%20CEST%0A
To create proper JSON from a shell script and properly escape variables:
jq -n --arg foobaz "$FOOBAZ" '{"foobaz":$foobaz}'
To fill environment variables from JSON object keys
(e.g. $FOO from jq query ".foo")
export $(jq -r '@sh "FOO=\(.foo) BAZ=\(.baz)"')
Parsing json
jq 'with_entries(.value |= fromjson)' --sort-keys
{ "b": "{}", "a": "{}" }
=> { "a": {}, "b": {} }
Serializing json
jq 'with_entries(.value |= tojson)' --sort-keys
{ "a": {}, "b": {} }
=> { "a": "{}", "b": "{}" }
Converting to csv
jq '.[] | [.foo, .bar] | @csv' -r
[{ "foo": 1, "bar": 2, "baz":3 }]
=> 1,2
List all keys of an object:
Slice an array from start to end index
jq '.[<start>:<end>]' file.json
Print selected fields joined as space-separated text
jq '.[] | [.field1, .field2] | join(" ")' file.json