| name | jq-json-processing |
| description | jq JSON processing: query, filter, transform JSON. Use when parsing JSON files, filtering arrays/objects, transforming structures, or extracting fields from JSON. |
| allowed-tools | Bash(jq *), Bash(cat *), Read, Write, Edit, Grep, Glob |
| metadata | {"upstream":{"user-invocable":false,"model":"sonnet"},"category":"data","source":{"repository":"https://github.com/laurigates/claude-plugins","path":"tools-plugin/skills/jq-json-processing","license_path":"LICENSE","commit":"6b5be230ee9a5a002b97094715f2ca877b08ad15"}} |
jq JSON Processing
Expert knowledge for processing, querying, and transforming JSON data using jq, the lightweight and flexible command-line JSON processor.
When to Use This Skill
| Use this skill when... | Use yq-yaml-processing instead when... |
|---|
| Extracting or filtering fields in JSON input | Working with YAML files (Kubernetes, GH Actions, Helm) |
Parsing gh, curl, or kubectl -o json responses | Editing YAML in place while preserving comments |
| CI pipelines on minimal images (jq is universally installed) | Converting YAML → JSON before processing |
| Use this skill when... | Use nushell-data-processing instead when... |
|---|
| One-off pipeline transforms that fit in a single expression | Multi-step transforms that span JSON, YAML, CSV, and TOML |
| Stdin-piped JSON from another command | Aggregations, group-by, or visual table exploration |
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
Essential Commands
Basic Querying
jq '.' file.json
jq '.fieldName' file.json
jq '.user.email' file.json
jq '.[0]' file.json
jq '.items[2]' file.json
Array Operations
jq '.items | length' file.json
jq '.items | map(.name)' file.json
jq '.items[] | select(.active == true)' file.json
jq '.users[] | select(.age > 18)' file.json
jq '.items | sort_by(.name)' file.json
jq '.items | sort_by(.date) | reverse' file.json
jq '.items | first' file.json
jq '.items | last' file.json
jq '.tags | unique' file.json
Object Operations
jq 'keys' file.json
jq '.config | keys' file.json
jq '.[] | values' file.json
jq '{name, email}' file.json
jq '{name: .fullName, id: .userId}' file.json
jq '. + {newField: "value"}' file.json
jq 'del(.password)' file.json
jq '. * {updated: true}' file.json
Filtering and Conditions
jq 'select(.status == "active")' file.json
jq '.[] | select(.price < 100)' file.json
jq '.[] | select(.active and .verified)' file.json
jq '.[] | select(.age > 18 and .country == "US")' file.json
jq '.[] | select(.type == "admin" or .type == "moderator")' file.json
jq '.[] | select(has("email"))' file.json
jq 'select(.optional != null)' file.json
jq '.[] | select(.status != "deleted")' file.json
String Operations
jq '"Hello, \(.name)"' file.json
jq '.id | tostring' file.json
jq '.[] | select(.email | contains("@gmail.com"))' file.json
jq '.[] | select(.name | startswith("A"))' file.json
jq '.[] | select(.file | endswith(".json"))' file.json
jq '.path | split("/")' file.json
jq '.tags | join(", ")' file.json
jq '.name | ascii_downcase' file.json
jq '.name | ascii_upcase' file.json
Pipes and Composition
jq '.items | map(.name) | sort | unique' file.json
jq '.users[] | select(.active) | select(.age > 18) | .email' file.json
jq 'group_by(.category)' file.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' file.json
Output Formatting
jq -c '.' file.json
jq -r '.message' file.json
jq -r '.items[] | .name' file.json
jq -r '.[] | [.name, .age, .email] | @tsv' file.json
jq -r '.[] | [.name, .age, .email] | @csv' file.json
jq -c '[.items[]]' file.json
Input/Output Options
cat file.json | jq '.items'
curl -s https://api.example.com/data | jq '.results'
jq -s '.' file1.json file2.json
jq '.filtered' input.json > output.json
jq '.updated = true' file.json | sponge file.json
Advanced Patterns
jq '.. | .email? // empty' file.json
jq '[.items[].price] | add' file.json
jq 'reduce .items[] as $item (0; . + $item.price)' file.json
jq '.items[] | . as $item | $item.name + " - " + ($item.price | tostring)' file.json
jq '.items[] | if .price > 100 then "expensive" else "affordable" end' file.json
jq 'if .error then .error else .data end' file.json
jq '.items[] | try .field catch "not found"' file.json
jq '.items | flatten' file.json
jq '.items | flatten(1)' file.json
Real-World Examples
jq '.. | .email? // empty' users.json
jq '[.items[].tags[]] | unique' data.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' items.json
jq '.results[] | {id, name: .full_name, active: .is_active}' response.json
gh run list --json status,conclusion,name,createdAt | \
jq '.[] | select(.conclusion == "failure") | {name, createdAt}'
jq '.dependencies | to_entries | map("\(.key)@\(.value)")' package.json
jq -s '.[0] * .[1]' base.json override.json
jq 'group_by(.level) | map({level: .[0].level, count: length, samples: [.[].message][:3]})' logs.json
Best Practices
Query Construction
- Start simple, build complexity incrementally
- Test filters on small datasets first
- Use
-c flag for compact output in scripts
- Use
-r flag for raw strings (no quotes)
Performance
- Use
select() early in pipeline to reduce data
- Use targeted queries with specific paths for large files
- Stream large JSON with
--stream flag
- Consider
jq -c for faster processing
Error Handling
- Use
? operator for optional access: .field?
- Use
// empty to filter out nulls/errors
- Use
try-catch for graceful error handling
- Check for field existence with
has("field")
Readability
- Break complex queries into multiple steps
- Use variables with
as $var for clarity
- Add comments in shell scripts
- Format multi-line jq programs for readability
Common Patterns
API Response Processing
gh pr list --json title,author,number | \
jq -r '.[] | "#\(.number) - \(.title) by @\(.author.login)"'
curl -s "https://api.example.com/items" | \
jq '.data.items[] | {id, name, status}'
Configuration Files
jq '.environments.production' config.json
jq '.settings.timeout = 30' config.json > config.updated.json
jq -s '.[0] * .[1]' base-config.json prod-config.json
Log Analysis
jq 'select(.level == "error") | .type' logs.json | sort | uniq -c
jq -r 'select(.level == "error") | "\(.timestamp) - \(.message)"' logs.json
jq -r '.timestamp | split("T")[1] | split(":")[0]' logs.json | sort | uniq -c
Data Transformation
jq -R -s 'split("\n") | .[1:] | map(split(",")) |
map({name: .[0], age: .[1], email: .[2]})' data.csv
jq -r '.[] | [.name, .age, .email] | @csv' data.json
jq '[.items[] | {id, name, category: .meta.category}]' nested.json
Troubleshooting
Invalid JSON
jq empty file.json
jq '.' file.json 2>&1 | grep "parse error"
Empty Results
jq '.' file.json
jq 'keys' file.json
jq 'type' file.json
jq '.. | scalars' file.json
Type Errors
jq '.field | type' file.json
jq '.id | tonumber' file.json
jq '.count | tostring' file.json
jq '.items[] | if type == "array" then .[] else . end' file.json
Performance Issues
jq --stream '.' large-file.json
cat large.json | jq -c '.[]' | while read -r line; do
echo "$line" | jq '.field'
done
Integration with Other Tools
curl -s "https://api.github.com/users/octocat" | jq '.name, .bio'
gh api repos/owner/repo/issues | jq '.[] | {number, title, state}'
find . -name "package.json" -exec jq '.version' {} \;
cat urls.txt | xargs -I {} curl -s {} | jq '.data'
yq eval -o=json file.yaml | jq '.specific.field'
Quick Reference
Operators
.field - Access field
.[] - Iterate array/object
| - Pipe (chain operations)
, - Multiple outputs
? - Optional (suppress errors)
// - Alternative operator (default value)
Functions
keys, values - Object keys/values
length - Array/object/string length
map(), select() - Array operations
sort, sort_by() - Sorting
group_by() - Grouping
unique - Remove duplicates
add - Sum numbers or concatenate
has() - Check field existence
type - Get value type
Type Conversions
tostring, tonumber - Convert types
@csv, @tsv, @json - Format output
split(), join() - String/array conversion
Installation
brew install jq
sudo apt-get install jq
jq --version
Resources