jq-json-processing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesejq JSON Processing
jq JSON 数据处理
Expert knowledge for processing, querying, and transforming JSON data using jq, the lightweight and flexible command-line JSON processor.
本文提供使用jq(轻量灵活的命令行JSON处理器)进行JSON数据处理、查询和转换的专业知识。
Core Expertise
核心能力
JSON Operations
- Query and filter JSON with path expressions
- Transform JSON structure and shape
- Combine, merge, and split JSON data
- Validate JSON syntax and structure
Data Extraction
- Extract specific fields from JSON objects
- Filter arrays based on conditions
- Navigate nested JSON structures
- Handle null values and missing keys
JSON 操作
- 使用路径表达式查询和过滤JSON
- 转换JSON的结构和形态
- 合并、拆分JSON数据
- 验证JSON语法和结构
数据提取
- 从JSON对象中提取特定字段
- 根据条件过滤数组
- 遍历嵌套的JSON结构
- 处理空值和缺失的键
Essential Commands
常用命令
Basic Querying
基础查询
bash
undefinedbash
undefinedPretty-print JSON
Pretty-print JSON
jq '.' file.json
jq '.' file.json
Extract specific field
Extract specific field
jq '.fieldName' file.json
jq '.fieldName' file.json
Extract nested field
Extract nested field
jq '.user.email' file.json
jq '.user.email' file.json
Extract array element
Extract array element
jq '.[0]' file.json
jq '.items[2]' file.json
undefinedjq '.[0]' file.json
jq '.items[2]' file.json
undefinedArray Operations
数组操作
bash
undefinedbash
undefinedGet array length
Get array length
jq '.items | length' file.json
jq '.items | length' file.json
Map over array
Map over array
jq '.items | map(.name)' file.json
jq '.items | map(.name)' file.json
Filter array
Filter array
jq '.items[] | select(.active == true)' file.json
jq '.users[] | select(.age > 18)' file.json
jq '.items[] | select(.active == true)' file.json
jq '.users[] | select(.age > 18)' file.json
Sort array
Sort array
jq '.items | sort_by(.name)' file.json
jq '.items | sort_by(.date) | reverse' file.json
jq '.items | sort_by(.name)' file.json
jq '.items | sort_by(.date) | reverse' file.json
Get first/last elements
Get first/last elements
jq '.items | first' file.json
jq '.items | last' file.json
jq '.items | first' file.json
jq '.items | last' file.json
Unique values
Unique values
jq '.tags | unique' file.json
undefinedjq '.tags | unique' file.json
undefinedObject Operations
对象操作
bash
undefinedbash
undefinedGet all keys
Get all keys
jq 'keys' file.json
jq '.config | keys' file.json
jq 'keys' file.json
jq '.config | keys' file.json
Get all values
Get all values
jq '.[] | values' file.json
jq '.[] | values' file.json
Select specific fields
Select specific fields
jq '{name, email}' file.json
jq '{name: .fullName, id: .userId}' file.json
jq '{name, email}' file.json
jq '{name: .fullName, id: .userId}' file.json
Add field
Add field
jq '. + {newField: "value"}' file.json
jq '. + {newField: "value"}' file.json
Delete field
Delete field
jq 'del(.password)' file.json
jq 'del(.password)' file.json
Merge objects
Merge objects
jq '. * {updated: true}' file.json
undefinedjq '. * {updated: true}' file.json
undefinedFiltering and Conditions
过滤与条件判断
bash
undefinedbash
undefinedSelect with conditions
Select with conditions
jq 'select(.status == "active")' file.json
jq '.[] | select(.price < 100)' file.json
jq 'select(.status == "active")' file.json
jq '.[] | select(.price < 100)' file.json
Multiple conditions (AND)
Multiple conditions (AND)
jq '.[] | select(.active and .verified)' file.json
jq '.[] | select(.age > 18 and .country == "US")' file.json
jq '.[] | select(.active and .verified)' file.json
jq '.[] | select(.age > 18 and .country == "US")' file.json
Multiple conditions (OR)
Multiple conditions (OR)
jq '.[] | select(.type == "admin" or .type == "moderator")' file.json
jq '.[] | select(.type == "admin" or .type == "moderator")' file.json
Exists / has field
Exists / has field
jq '.[] | select(has("email"))' file.json
jq 'select(.optional != null)' file.json
jq '.[] | select(has("email"))' file.json
jq 'select(.optional != null)' file.json
Not condition
Not condition
jq '.[] | select(.status != "deleted")' file.json
undefinedjq '.[] | select(.status != "deleted")' file.json
undefinedString Operations
字符串操作
bash
undefinedbash
undefinedString interpolation
String interpolation
jq '"Hello, (.name)"' file.json
jq '"Hello, (.name)"' file.json
Convert to string
Convert to string
jq '.id | tostring' file.json
jq '.id | tostring' file.json
String contains
String contains
jq '.[] | select(.email | contains("@gmail.com"))' file.json
jq '.[] | select(.email | contains("@gmail.com"))' file.json
String starts/ends with
String starts/ends with
jq '.[] | select(.name | startswith("A"))' file.json
jq '.[] | select(.file | endswith(".json"))' file.json
jq '.[] | select(.name | startswith("A"))' file.json
jq '.[] | select(.file | endswith(".json"))' file.json
Split string
Split string
jq '.path | split("/")' file.json
jq '.path | split("/")' file.json
Join array to string
Join array to string
jq '.tags | join(", ")' file.json
jq '.tags | join(", ")' file.json
Lowercase/uppercase
Lowercase/uppercase
jq '.name | ascii_downcase' file.json
jq '.name | ascii_upcase' file.json
undefinedjq '.name | ascii_downcase' file.json
jq '.name | ascii_upcase' file.json
undefinedPipes and Composition
管道与组合操作
bash
undefinedbash
undefinedChain operations
Chain operations
jq '.items | map(.name) | sort | unique' file.json
jq '.items | map(.name) | sort | unique' file.json
Multiple filters
Multiple filters
jq '.users[] | select(.active) | select(.age > 18) | .email' file.json
jq '.users[] | select(.active) | select(.age > 18) | .email' file.json
Group by
Group by
jq 'group_by(.category)' file.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' file.json
undefinedjq 'group_by(.category)' file.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' file.json
undefinedOutput Formatting
输出格式化
bash
undefinedbash
undefinedCompact output (no pretty-print)
Compact output (no pretty-print)
jq -c '.' file.json
jq -c '.' file.json
Raw output (no quotes for strings)
Raw output (no quotes for strings)
jq -r '.message' file.json
jq -r '.items[] | .name' file.json
jq -r '.message' file.json
jq -r '.items[] | .name' file.json
Output as tab-separated values
Output as tab-separated values
jq -r '.[] | [.name, .age, .email] | @tsv' file.json
jq -r '.[] | [.name, .age, .email] | @tsv' file.json
Output as CSV
Output as CSV
jq -r '.[] | [.name, .age, .email] | @csv' file.json
jq -r '.[] | [.name, .age, .email] | @csv' file.json
Output as JSON array on one line
Output as JSON array on one line
jq -c '[.items[]]' file.json
undefinedjq -c '[.items[]]' file.json
undefinedInput/Output Options
输入/输出选项
bash
undefinedbash
undefinedRead from stdin
Read from stdin
cat file.json | jq '.items'
curl -s https://api.example.com/data | jq '.results'
cat file.json | jq '.items'
curl -s https://api.example.com/data | jq '.results'
Multiple input files
Multiple input files
jq -s '.' file1.json file2.json # Slurp into array
jq -s '.' file1.json file2.json # Slurp into array
Write to file
Write to file
jq '.filtered' input.json > output.json
jq '.filtered' input.json > output.json
In-place edit (use sponge from moreutils)
In-place edit (use sponge from moreutils)
jq '.updated = true' file.json | sponge file.json
undefinedjq '.updated = true' file.json | sponge file.json
undefinedAdvanced Patterns
高级模式
bash
undefinedbash
undefinedRecursive descent
Recursive descent
jq '.. | .email? // empty' file.json
jq '.. | .email? // empty' file.json
Reduce (sum, accumulate)
Reduce (sum, accumulate)
jq '[.items[].price] | add' file.json
jq 'reduce .items[] as $item (0; . + $item.price)' file.json
jq '[.items[].price] | add' file.json
jq 'reduce .items[] as $item (0; . + $item.price)' file.json
Variable assignment
Variable assignment
jq '.items[] | . as $item | $item.name + " - " + ($item.price | tostring)' file.json
jq '.items[] | . as $item | $item.name + " - " + ($item.price | tostring)' file.json
Conditional (if-then-else)
Conditional (if-then-else)
jq '.items[] | if .price > 100 then "expensive" else "affordable" end' file.json
jq 'if .error then .error else .data end' file.json
jq '.items[] | if .price > 100 then "expensive" else "affordable" end' file.json
jq 'if .error then .error else .data end' file.json
Try-catch for error handling
Try-catch for error handling
jq '.items[] | try .field catch "not found"' file.json
jq '.items[] | try .field catch "not found"' file.json
Flatten nested arrays
Flatten nested arrays
jq '.items | flatten' file.json
jq '.items | flatten(1)' file.json # Flatten one level
undefinedjq '.items | flatten' file.json
jq '.items | flatten(1)' file.json # Flatten one level
undefinedReal-World Examples
实际应用示例
bash
undefinedbash
undefinedExtract all emails from nested structure
Extract all emails from nested structure
jq '.. | .email? // empty' users.json
jq '.. | .email? // empty' users.json
Get unique list of all tags across items
Get unique list of all tags across items
jq '[.items[].tags[]] | unique' data.json
jq '[.items[].tags[]] | unique' data.json
Count items by status
Count items by status
jq 'group_by(.status) | map({status: .[0].status, count: length})' items.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' items.json
Transform API response to simple list
Transform API response to simple list
jq '.results[] | {id, name: .full_name, active: .is_active}' response.json
jq '.results[] | {id, name: .full_name, active: .is_active}' response.json
Filter GitHub workflow runs (recent failures)
Filter GitHub workflow runs (recent failures)
gh run list --json status,conclusion,name,createdAt |
jq '.[] | select(.conclusion == "failure") | {name, createdAt}'
jq '.[] | select(.conclusion == "failure") | {name, createdAt}'
gh run list --json status,conclusion,name,createdAt |
jq '.[] | select(.conclusion == "failure") | {name, createdAt}'
jq '.[] | select(.conclusion == "failure") | {name, createdAt}'
Extract package.json dependencies with versions
Extract package.json dependencies with versions
jq '.dependencies | to_entries | map("(.key)@(.value)")' package.json
jq '.dependencies | to_entries | map("(.key)@(.value)")' package.json
Merge two JSON files
Merge two JSON files
jq -s '.[0] * .[1]' base.json override.json
jq -s '.[0] * .[1]' base.json override.json
Create summary from log data
Create summary from log data
jq 'group_by(.level) | map({level: .[0].level, count: length, samples: [.[].message][:3]})' logs.json
undefinedjq 'group_by(.level) | map({level: .[0].level, count: length, samples: [.[].message][:3]})' logs.json
undefinedBest Practices
最佳实践
Query Construction
- Start simple, build complexity incrementally
- Test filters on small datasets first
- Use flag for compact output in scripts
-c - Use flag for raw strings (no quotes)
-r
Performance
- Use early in pipeline to reduce data
select() - Use targeted queries with specific paths for large files
- Stream large JSON with flag
--stream - Consider for faster processing
jq -c
Error Handling
- Use operator for optional access:
?.field? - Use to filter out nulls/errors
// empty - Use for graceful error handling
try-catch - Check for field existence with
has("field")
Readability
- Break complex queries into multiple steps
- Use variables with for clarity
as $var - Add comments in shell scripts
- Format multi-line jq programs for readability
查询构建
- 从简单查询开始,逐步增加复杂度
- 先在小数据集上测试过滤器
- 在脚本中使用参数获取紧凑输出
-c - 使用参数获取原始字符串(无引号)
-r
性能优化
- 在管道中尽早使用减少数据量
select() - 对大文件使用带具体路径的定向查询
- 使用参数处理流式大JSON
--stream - 考虑使用提升处理速度
jq -c
错误处理
- 使用操作符进行可选访问:
?.field? - 使用过滤空值/错误
// empty - 使用实现优雅的错误处理
try-catch - 使用检查字段是否存在
has("field")
可读性
- 将复杂查询拆分为多个步骤
- 使用定义变量提升清晰度
as $var - 在Shell脚本中添加注释
- 对多行jq程序进行格式化以提升可读性
Common Patterns
常见使用模式
API Response Processing
API响应处理
bash
undefinedbash
undefinedGitHub API: Get PR titles and authors
GitHub API: Get PR titles and authors
gh pr list --json title,author,number |
jq -r '.[] | "#(.number) - (.title) by @(.author.login)"'
jq -r '.[] | "#(.number) - (.title) by @(.author.login)"'
gh pr list --json title,author,number |
jq -r '.[] | "#(.number) - (.title) by @(.author.login)"'
jq -r '.[] | "#(.number) - (.title) by @(.author.login)"'
REST API: Extract and flatten pagination
REST API: Extract and flatten pagination
curl -s "https://api.example.com/items" |
jq '.data.items[] | {id, name, status}'
jq '.data.items[] | {id, name, status}'
undefinedcurl -s "https://api.example.com/items" |
jq '.data.items[] | {id, name, status}'
jq '.data.items[] | {id, name, status}'
undefinedConfiguration Files
配置文件处理
bash
undefinedbash
undefinedExtract environment-specific config
Extract environment-specific config
jq '.environments.production' config.json
jq '.environments.production' config.json
Update configuration value
Update configuration value
jq '.settings.timeout = 30' config.json > config.updated.json
jq '.settings.timeout = 30' config.json > config.update.json
Merge base config with environment overrides
Merge base config with environment overrides
jq -s '.[0] * .[1]' base-config.json prod-config.json
undefinedjq -s '.[0] * .[1]' base-config.json prod-config.json
undefinedLog Analysis
日志分析
bash
undefinedbash
undefinedCount errors by type
Count errors by type
jq 'select(.level == "error") | .type' logs.json | sort | uniq -c
jq 'select(.level == "error") | .type' logs.json | sort | uniq -c
Extract error messages with timestamps
Extract error messages with timestamps
jq -r 'select(.level == "error") | "(.timestamp) - (.message)"' logs.json
jq -r 'select(.level == "error") | "(.timestamp) - (.message)"' logs.json
Group by hour and count
Group by hour and count
jq -r '.timestamp | split("T")[1] | split(":")[0]' logs.json | sort | uniq -c
undefinedjq -r '.timestamp | split("T")[1] | split(":")[0]' logs.json | sort | uniq -c
undefinedData Transformation
数据转换
bash
undefinedbash
undefinedCSV to JSON (with headers)
CSV to JSON (with headers)
jq -R -s 'split("\n") | .[1:] | map(split(",")) |
map({name: .[0], age: .[1], email: .[2]})' data.csv
jq -R -s 'split("\n") | .[1:] | map(split(",")) |
map({name: .[0], age: .[1], email: .[2]})' data.csv
JSON to CSV
JSON to CSV
jq -r '.[] | [.name, .age, .email] | @csv' data.json
jq -r '.[] | [.name, .age, .email] | @csv' data.json
Flatten nested structure
Flatten nested structure
jq '[.items[] | {id, name, category: .meta.category}]' nested.json
undefinedjq '[.items[] | {id, name, category: .meta.category}]' nested.json
undefinedTroubleshooting
问题排查
Invalid JSON
无效JSON
bash
undefinedbash
undefinedValidate JSON syntax
Validate JSON syntax
jq empty file.json # Returns exit code 0 if valid
jq empty file.json # Returns exit code 0 if valid
Find syntax errors
Find syntax errors
jq '.' file.json 2>&1 | grep "parse error"
undefinedjq '.' file.json 2>&1 | grep "parse error"
undefinedEmpty Results
无结果返回
bash
undefinedbash
undefinedDebug: Print entire structure
Debug: Print entire structure
jq '.' file.json
jq '.' file.json
Debug: Check field existence
Debug: Check field existence
jq 'keys' file.json
jq 'type' file.json # Check if array, object, etc.
jq 'keys' file.json
jq 'type' file.json # Check if array, object, etc.
Debug: Show all values
Debug: Show all values
jq '.. | scalars' file.json
undefinedjq '.. | scalars' file.json
undefinedType Errors
类型错误
bash
undefinedbash
undefinedCheck field types
Check field types
jq '.field | type' file.json
jq '.field | type' file.json
Convert types safely
Convert types safely
jq '.id | tonumber' file.json
jq '.count | tostring' file.json
jq '.id | tonumber' file.json
jq '.count | tostring' file.json
Handle mixed types
Handle mixed types
jq '.items[] | if type == "array" then .[] else . end' file.json
undefinedjq '.items[] | if type == "array" then .[] else . end' file.json
undefinedPerformance Issues
性能问题
bash
undefinedbash
undefinedStream large files
Stream large files
jq --stream '.' large-file.json
jq --stream '.' large-file.json
Process line by line
Process line by line
cat large.json | jq -c '.[]' | while read -r line; do
echo "$line" | jq '.field'
done
undefinedcat large.json | jq -c '.[]' | while read -r line; do
echo "$line" | jq '.field'
done
undefinedIntegration with Other Tools
与其他工具集成
bash
undefinedbash
undefinedWith curl (API calls)
With curl (API calls)
curl -s "https://api.github.com/users/octocat" | jq '.name, .bio'
curl -s "https://api.github.com/users/octocat" | jq '.name, .bio'
With gh CLI (GitHub operations)
With gh CLI (GitHub operations)
gh api repos/owner/repo/issues | jq '.[] | {number, title, state}'
gh api repos/owner/repo/issues | jq '.[] | {number, title, state}'
With find (batch processing)
With find (batch processing)
find . -name "package.json" -exec jq '.version' {} ;
find . -name "package.json" -exec jq '.version' {} ;
With xargs (parallel processing)
With xargs (parallel processing)
cat urls.txt | xargs -I {} curl -s {} | jq '.data'
cat urls.txt | xargs -I {} curl -s {} | jq '.data'
With yq (YAML to JSON conversion)
With yq (YAML to JSON conversion)
yq eval -o=json file.yaml | jq '.specific.field'
undefinedyq eval -o=json file.yaml | jq '.specific.field'
undefinedQuick Reference
速查指南
Operators
操作符
- - Access field
.field - - Iterate array/object
.[] - - Pipe (chain operations)
| - - Multiple outputs
, - - Optional (suppress errors)
? - - Alternative operator (default value)
//
- - 访问字段
.field - - 遍历数组/对象
.[] - - 管道(链式操作)
| - - 多输出
, - - 可选访问(抑制错误)
? - - 替代操作符(默认值)
//
Functions
函数
- ,
keys- Object keys/valuesvalues - - Array/object/string length
length - ,
map()- Array operationsselect() - ,
sort- Sortingsort_by() - - Grouping
group_by() - - Remove duplicates
unique - - Sum numbers or concatenate
add - - Check field existence
has() - - Get value type
type
- ,
keys- 对象键/值values - - 数组/对象/字符串长度
length - ,
map()- 数组操作select() - ,
sort- 排序sort_by() - - 分组
group_by() - - 去重
unique - - 数值求和或字符串拼接
add - - 检查字段存在性
has() - - 获取值类型
type
Type Conversions
类型转换
- ,
tostring- Convert typestonumber - ,
@csv,@tsv- Format output@json - ,
split()- String/array conversionjoin()
- ,
tostring- 类型转换tonumber - ,
@csv,@tsv- 输出格式化@json - ,
split()- 字符串/数组转换join()
Installation
安装方法
bash
undefinedbash
undefinedmacOS (Homebrew)
macOS (Homebrew)
brew install jq
brew install jq
Ubuntu/Debian
Ubuntu/Debian
sudo apt-get install jq
sudo apt-get install jq
Verify installation
Verify installation
jq --version
undefinedjq --version
undefinedResources
参考资源
- Official Manual: https://stedolan.github.io/jq/manual/
- jq Playground: https://jqplay.org/
- Tutorial: https://stedolan.github.io/jq/tutorial/
- 官方手册: https://stedolan.github.io/jq/manual/
- jq 在线 playground: https://jqplay.org/
- 教程: https://stedolan.github.io/jq/tutorial/