Last updated
Example: Array Operations
// Input JSON:
{
"users": [
{ "id": 1, "name": "Alice", "active": true, "score": 85 },
{ "id": 2, "name": "Bob", "active": false, "score": 72 },
{ "id": 3, "name": "Carol", "active": true, "score": 91 },
{ "id": 4, "name": "Dave", "active": true, "score": 68 }
]
}
// Get all users: .users[]
// Output: each user object on separate lines
// Get all names: .users[].name
// Output: "Alice" "Bob" "Carol" "Dave"
// Get names as array: [.users[].name]
// Output: ["Alice", "Bob", "Carol", "Dave"]
// Filter active users: .users[] | select(.active == true)
// Output: Alice, Carol, Dave objects
// Filter and extract: [.users[] | select(.active) | .name]
// Output: ["Alice", "Carol", "Dave"]
// Sort by score: .users | sort_by(.score)
// Output: array sorted Dave, Bob, Alice, Carol
// Sort descending: .users | sort_by(.score) | reverse
// Output: Carol, Alice, Bob, Dave
Example: Transforming Data
// Input JSON:
[
{ "first": "Alice", "last": "Smith", "dept": "Engineering", "salary": 95000 },
{ "first": "Bob", "last": "Jones", "dept": "Marketing", "salary": 72000 },
{ "first": "Carol", "last": "Brown", "dept": "Engineering", "salary": 105000 },
{ "first": "Dave", "last": "Wilson", "dept": "Marketing", "salary": 68000 }
]
// Create full name field:
// map({ fullName: "\(.first) \(.last)", dept, salary })
// Output:
[
{ "fullName": "Alice Smith", "dept": "Engineering", "salary": 95000 },
{ "fullName": "Bob Jones", "dept": "Marketing", "salary": 72000 },
{ "fullName": "Carol Brown", "dept": "Engineering", "salary": 105000 },
{ "fullName": "Dave Wilson", "dept": "Marketing", "salary": 68000 }
]
// Group by department:
// group_by(.dept) | map({ dept: .[0].dept, members: map(.first) })
// Output:
[
{ "dept": "Engineering", "members": ["Alice", "Carol"] },
{ "dept": "Marketing", "members": ["Bob", "Dave"] }
]
// Average salary by department:
// group_by(.dept) | map({ dept: .[0].dept, avgSalary: (map(.salary) | add / length) })
// Output:
[
{ "dept": "Engineering", "avgSalary": 100000 },
{ "dept": "Marketing", "avgSalary": 70000 }
]
Example: Working with Nested Arrays
// Input JSON:
{
"orders": [
{
"id": "ORD-001",
"items": [
{ "product": "Widget", "qty": 2, "price": 10.00 },
{ "product": "Gadget", "qty": 1, "price": 25.00 }
]
},
{
"id": "ORD-002",
"items": [
{ "product": "Widget", "qty": 5, "price": 10.00 }
]
}
]
}
// Get all order IDs: [.orders[].id]
// Output: ["ORD-001", "ORD-002"]
// Calculate order totals:
// .orders | map({ id, total: (.items | map(.qty * .price) | add) })
// Output:
[
{ "id": "ORD-001", "total": 45 },
{ "id": "ORD-002", "total": 50 }
]
// Flatten all items across orders:
// [.orders[].items[]]
// Output: all 3 item objects in a flat array
Example: String Operations
// Input: { "name": "hello world", "tags": "js,python,go" }
// Uppercase: .name | ascii_upcase
// Output: "HELLO WORLD"
// Split string: .tags | split(",")
// Output: ["js", "python", "go"]
// String interpolation: "Name: \(.name)"
// Output: "Name: hello world"
// Test with regex: .name | test("hello")
// Output: true
// Extract with regex: .name | capture("(?<first>\\w+) (?<second>\\w+)")
// Output: { "first": "hello", "second": "world" }
Example: Conditional Logic
// Input: array of products with stock levels
// Classify stock level:
// .products | map({
// name,
// status: (if .stock == 0 then "out_of_stock"
// elif .stock < 10 then "low_stock"
// else "in_stock" end)
// })
// Filter with multiple conditions:
// .products[] | select(.price > 50 and .stock > 0)
// Null handling with alternative operator:
// .user.nickname // .user.name // "Anonymous"
// Returns nickname if set, falls back to name, then "Anonymous"
Example: Generating Shell Commands
// After developing your query in the playground,
// get the equivalent shell command:
// Query: [.users[] | select(.active) | .name]
// Shell command:
cat data.json | jq '[.users[] | select(.active) | .name]'
// Or with a file:
jq '[.users[] | select(.active) | .name]' data.json
// Pretty-print output (default):
jq '.' data.json
// Compact output:
jq -c '.' data.json
// Raw string output (no quotes):
jq -r '.name' data.json
Common jq Functions Reference
- select(condition) — filter items matching condition
- map(expr) — transform each item in array
- sort_by(.field) — sort array by field value
- group_by(.field) — group array items by field
- unique_by(.field) — deduplicate by field
- keys — get object keys as array
- values — get object values as array
- has("key") — check if key exists
- length — count items or string length
- add — sum array or concatenate strings
- to_entries / from_entries — convert object to/from key-value pairs
- ascii_downcase / ascii_upcase — string case conversion
Paste your JSON into the left panel, write your jq query in the middle, and see the result instantly in the right panel. Use the function reference to discover available operations.
Example: Basic Field Selection
// Input JSON:
{
"name": "Alice",
"age": 30,
"email": "alice@example.com",
"address": {
"city": "New York",
"zip": "10001"
}
}
// Query: .name
// Output: "Alice"
// Query: .address.city
// Output: "New York"
// Query: .address
// Output: { "city": "New York", "zip": "10001" }
// Query: { name: .name, city: .address.city }
// Output: { "name": "Alice", "city": "New York" }