Linux
jq
Command-line JSON processor.
Basic Operations
Pretty Print
jq '.' [file]
Compact Output
jq -c '.' [file]
Raw Output (No Quotes)
jq -r '.' [file]
Selecting Fields
Single Field
jq '.name' [file]
Nested Field
jq '.user.name' [file]
Multiple Fields
jq '.name, .age' [file]
Create Object with Selected Fields
jq '{name: .name, age: .age}' [file]
Array Operations
Get Array Length
jq '.items | length' [file]
First Element
jq '.items[0]' [file]
Last Element
jq '.items[-1]' [file]
Slice Array
jq '.items[2:5]' [file]
All Elements
jq '.items[]' [file]
Iterate and Extract Field
jq '.items[].name' [file]
Filtering
Select by Condition
jq '.items[] | select(.age > 18)' [file]
Filter by String Match
jq '.items[] | select(.name == "John")' [file]
Contains
jq '.items[] | select(.tags | contains(["linux"]))' [file]
Not Null
jq '.items[] | select(.email != null)' [file]
Mapping
Map Array
jq '.items | map(.name)' [file]
Transform Values
jq '.items | map({name: .name, uppercase: (.name | ascii_upcase)})' [file]
Map with Select
jq '.items | map(select(.active == true))' [file]
Sorting
Sort Array
jq '.items | sort' [file]
Sort by Field
jq '.items | sort_by(.age)' [file]
Reverse Sort
jq '.items | sort_by(.age) | reverse' [file]
Grouping
Group By Field
jq 'group_by(.category)' [file]
Unique Values
jq '.items | map(.category) | unique' [file]
Aggregation
Min/Max
jq '.items | map(.age) | min' [file]
jq '.items | map(.age) | max' [file]
Sum
jq '.items | map(.price) | add' [file]
Average
jq '.items | map(.age) | add / length' [file]
Conditionals
If-Then-Else
jq '.items[] | if .age >= 18 then "adult" else "minor" end' [file]
Ternary
jq '.items[] | .status = (if .active then "on" else "off" end)' [file]
String Operations
Concatenate
jq '.first_name + " " + .last_name' [file]
Split String
jq '.email | split("@")' [file]
Replace
jq '.text | gsub("old"; "new")' [file]
To Uppercase
jq '.name | ascii_upcase' [file]
To Lowercase
jq '.name | ascii_downcase' [file]
Object Manipulation
Add Field
jq '. + {new_field: "value"}' [file]
Delete Field
jq 'del(.field_name)' [file]
Get Keys
jq 'keys' [file]
Get Values
jq 'values' [file]
Merge Objects
jq '. * {override: "value"}' [file]
Type Conversion
To String
jq '.age | tostring' [file]
To Number
jq '.id | tonumber' [file]
To Array
jq '. | to_entries' [file]
From Array
jq 'from_entries' [file]
Input/Output
Read from stdin
echo '{"name":"John"}' | jq '.name'
Multiple JSON Documents
jq -s '.' file1.json file2.json
Write to File
jq '.' input.json > output.json
Tab-Separated Values
jq -r '.items[] | [.name, .age] | @tsv' [file]
CSV Output
jq -r '.items[] | [.name, .age] | @csv' [file]
Advanced Examples
Extract Nested Array Values
jq '.users[].orders[].id' [file]
Flatten Array
jq '[.[] | .items[]]' [file]
Recursive Descent
jq '.. | .email? | select(. != null)' [file]
Complex Filter
jq '.items[] | select(.price > 100 and .available == true) | {name, price}' [file]
Group and Count
jq 'group_by(.category) | map({category: .[0].category, count: length})' [file]
Working with APIs
Fetch and Parse
curl -s https://api.example.com/data | jq '.results[]'
Extract Field from API Response
curl -s https://api.github.com/users/github | jq '.name'
Filter and Format
curl -s https://api.example.com/users | jq '.[] | select(.active) | .email'
Common Use Cases
Pretty Print JSON
cat [file] | jq '.'
Extract All Email Addresses
jq '.users[].email' [file]
Count Items
jq '.items | length' [file]
Get Unique Tags
jq '[.items[].tags[]] | unique' [file]
Find by ID
jq '.items[] | select(.id == 123)' [file]
Convert to CSV
jq -r '.items[] | [.id, .name, .email] | @csv' [file]
Validate JSON
jq empty [file] && echo "Valid JSON" || echo "Invalid JSON"
Tips
- Use
-rfor raw output (without quotes) - Use
-cfor compact output - Use
-sto slurp multiple JSON objects into array - Pipe through
jq '.'to validate JSON - Use
select()for filtering - Combine with
curlfor API data processing