tldr/pages/common/jq.md

28 lines
650 B
Markdown
Raw Normal View History

2016-12-21 23:40:12 +00:00
# jq
> A lightweight and flexible command-line JSON processor.
2017-05-04 09:35:38 +01:00
- Output a JSON file, in pretty-print format:
2016-12-21 23:40:12 +00:00
`cat {{file}} | jq`
2017-05-04 09:35:38 +01:00
- Output all elements from arrays (or all key-value pairs from objects) in a JSON file:
2016-12-21 23:40:12 +00:00
`cat {{file}} | jq .[]`
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
`cat {{file}} | jq --slurp`
2017-05-04 09:35:38 +01:00
- Output the first element in a JSON file:
2016-12-21 23:40:12 +00:00
`cat {{file}} | jq .[0]`
2017-05-04 09:35:38 +01:00
- Output the value of a given key of the first element in a JSON file:
2016-12-21 23:40:12 +00:00
2017-05-04 09:35:38 +01:00
`cat {{file}} | jq .[0].{{key_name}}`
2016-12-21 23:40:12 +00:00
2017-05-04 09:35:38 +01:00
- Output the value of a given key of each element in a JSON file:
2016-12-21 23:40:12 +00:00
2017-05-04 09:35:38 +01:00
`cat {{file}} | jq 'map(.{{key_name}})'`