Linux command
jq 命令
文本
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Pretty print JSON
cat [data.json] | jq '.'
Extract field
cat [data.json] | jq '.name'
Array indexing
cat [data.json] | jq '.[0]'
Filter array
cat [data.json] | jq '.[] | select(.age > 30)'
Map transformation
cat [data.json] | jq '[.[] | {name, age}]'
Raw string output
cat [data.json] | jq -r '.name'
Compact output
cat [data.json] | jq -c '.'
说明
jq is a lightweight, command-line JSON processor often described as "sed for JSON." It reads JSON input from files or standard input, applies a filter expression, and writes the transformed result to standard output. Filters can be chained together with the pipe operator (`|`), enabling multi-step transformations that extract fields, restructure objects, compute values, and aggregate arrays in a single invocation. The filter language supports object and array indexing (`.foo`, `.0`), iteration (`.[]`), conditionals (`if-then-else`), comparison and logic operators, built-in functions like `map`, `select`, `group_by`, and `sort_by`, as well as string interpolation and regular expressions. This makes jq well suited for tasks like extracting nested values from API responses, converting JSON records into CSV, or reshaping data for downstream tools. Output can be pretty-printed (the default), compacted with `-c` for piping, or emitted as raw strings with `-r` for shell integration.
参数
- -r, --raw-output
- Output raw strings.
- -c, --compact-output
- Compact output.
- -s, --slurp
- Read entire input as array.
- -n, --null-input
- No input.
- --arg _NAME_ _VAL_
- Set variable.
- --help
- Display help information.
FAQ
What is the jq command used for?
jq is a lightweight, command-line JSON processor often described as "sed for JSON." It reads JSON input from files or standard input, applies a filter expression, and writes the transformed result to standard output. Filters can be chained together with the pipe operator (`|`), enabling multi-step transformations that extract fields, restructure objects, compute values, and aggregate arrays in a single invocation. The filter language supports object and array indexing (`.foo`, `.0`), iteration (`.[]`), conditionals (`if-then-else`), comparison and logic operators, built-in functions like `map`, `select`, `group_by`, and `sort_by`, as well as string interpolation and regular expressions. This makes jq well suited for tasks like extracting nested values from API responses, converting JSON records into CSV, or reshaping data for downstream tools. Output can be pretty-printed (the default), compacted with `-c` for piping, or emitted as raw strings with `-r` for shell integration.
How do I run a basic jq example?
Run `cat [data.json] | jq '.'` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -r, --raw-output do in jq?
Output raw strings.