2016-12-21 23:40:12 +00:00
|
|
|
# jq
|
|
|
|
|
2020-02-17 23:07:44 +00:00
|
|
|
> A command-line JSON processor that uses a domain-specific language.
|
2019-06-05 08:55:42 +01:00
|
|
|
> More information: <https://stedolan.github.io/jq>.
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2017-05-04 09:35:38 +01:00
|
|
|
- Output a JSON file, in pretty-print format:
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2018-04-13 19:04:03 +01:00
|
|
|
`jq . {{file.json}}`
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2020-08-28 09:25:06 +01:00
|
|
|
- Output all elements from arrays (or all the values from objects) in a JSON file:
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2019-06-27 10:08:39 +01:00
|
|
|
`jq '.[]' {{file.json}}`
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2022-03-12 04:08:08 +00:00
|
|
|
- Output elements from arrays which match provided filter:
|
|
|
|
|
|
|
|
`jq '.[] | select(.{{key_name}} == {{value}})'`
|
|
|
|
|
2017-05-04 09:35:38 +01:00
|
|
|
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2018-04-13 19:04:03 +01:00
|
|
|
`jq --slurp . {{file.json}}`
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2017-05-04 09:35:38 +01:00
|
|
|
- Output the first element in a JSON file:
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2019-06-27 10:08:39 +01:00
|
|
|
`jq '.[0]' {{file.json}}`
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2021-01-31 17:05:18 +00:00
|
|
|
- Output the value of a given key of each element in a JSON text from stdin:
|
2016-12-21 23:40:12 +00:00
|
|
|
|
2018-04-13 19:04:03 +01:00
|
|
|
`cat {{file.json}} | jq 'map(.{{key_name}})'`
|
2019-04-17 15:55:23 +01:00
|
|
|
|
2020-07-17 18:37:49 +01:00
|
|
|
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
|
|
|
|
|
2020-08-11 16:49:43 +01:00
|
|
|
`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'`
|
2020-07-17 18:37:49 +01:00
|
|
|
|
2019-04-22 21:22:29 +01:00
|
|
|
- Output the value of a given key to a string (and disable JSON output):
|
|
|
|
|
|
|
|
`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'`
|