1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-04-29 23:24:55 +02:00
tldr/pages/common/jq.md
Lucas Gabriel Schneider a5fe31bc47
multiple pages: format technical tokens (#5119)
Co-authored-by: bl-ue <54780737+bl-ue@users.noreply.github.com>
Co-authored-by: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
2021-01-31 12:05:18 -05:00

36 lines
1 KiB
Markdown

# jq
> A command-line JSON processor that uses a domain-specific language.
> More information: <https://stedolan.github.io/jq>.
- Output a JSON file, in pretty-print format:
`jq . {{file.json}}`
- Output all elements from arrays (or all the values from objects) in a JSON file:
`jq '.[]' {{file.json}}`
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
`jq --slurp . {{file.json}}`
- Output the first element in a JSON file:
`jq '.[0]' {{file.json}}`
- Output the value of a given key of each element in a JSON text from stdin:
`cat {{file.json}} | jq 'map(.{{key_name}})'`
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'`
- Combine multiple filters:
`cat {{file.json}} | jq 'unique | sort | reverse'`
- Output the value of a given key to a string (and disable JSON output):
`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'`