tldr/pages/common/jq.md

33 lines
867 B
Markdown
Raw Normal View History

2016-12-21 23:40:12 +00:00
# jq
> A command-line JSON processor that uses a domain-specific language (DSL).
2024-02-24 19:42:40 +00:00
> More information: <https://jqlang.github.io/jq/manual/>.
2016-12-21 23:40:12 +00:00
- Execute a specific expression (print a colored and formatted JSON output):
2016-12-21 23:40:12 +00:00
`{{cat path/to/file.json}} | jq '.'`
2016-12-21 23:40:12 +00:00
- Execute a specific script:
2016-12-21 23:40:12 +00:00
`{{cat path/to/file.json}} | jq --from-file {{path/to/script.jq}}`
2016-12-21 23:40:12 +00:00
- Pass specific arguments:
`{{cat path/to/file.json}} | jq {{--arg "name1" "value1" --arg "name2" "value2" ...}} '{{. + $ARGS.named}}'`
- Print specific keys:
2016-12-21 23:40:12 +00:00
`{{cat path/to/file.json}} | jq '{{.key1, .key2, ...}}'`
2016-12-21 23:40:12 +00:00
- Print specific array items:
2016-12-21 23:40:12 +00:00
`{{cat path/to/file.json}} | jq '{{.[index1], .[index2], ...}}'`
2016-12-21 23:40:12 +00:00
2024-02-24 19:42:40 +00:00
- Print all array/object values:
2016-12-21 23:40:12 +00:00
`{{cat path/to/file.json}} | jq '.[]'`
- Add/remove specific keys:
`{{cat path/to/file.json}} | jq '. {{+|-}} {{{"key1": "value1", "key2": "value2", ...}}}'`