JSON 20 ๐ In-Memory Querying and Filtering with jq
jq is a command-line JSON processor, and its core model is the filter pipeline. The tool reads the JSON from the standard input or the files, and it applies the filter expression to the input, and it writes the result to the standard output. The filter is not a query language in the SQL sense. It is a functional expression that takes the input and produces the output, and the output can be the single value, the stream of the values, or the empty. The manual states the model plainly: “jq is a filter, and it takes its input and produces its output” . The jq is the tool for the in-memory querying and filtering, and the filter is the mechanism that selects, transforms, and constructs the JSON.
The tool’s power is in the composition. The . is the identity filter, the .foo is the field access, the .[] is the array or the object iterator, and the | is the pipe that feeds the output of one filter into the next . The , is the generator that runs both filters on the same input, the map() is the array transformation, and the select() is the conditional filter. The composition is the mechanism that makes the jq the general-purpose processor, and the processor is the reason the jq is the standard tool for the JSON manipulation in the shell.
Key point: jq reads the JSON and applies the filter expression. The . is the identity, the .foo is the field access, the .[] is the iterator, and the | is the pipe. The map() transforms the array, the select() filters the array, and the {} constructs the object. The tool is the standard for the JSON processing in the command line.
Why jq exists
The shell problem. The shell has the text toolsโthe grep, the sed, the awkโand the tools are line-oriented. The JSON is the nested structure, and the line-oriented tools cannot traverse the structure without the fragile parsing. The jq is the structure-aware processor, and the filter is the mechanism that selects the nested value without the manual parsing . The tool is the bridge between the shell and the structured data.
The transformation problem. The JSON data often needs the shape changeโthe array to the object, the object to the array, the subset of the fields, the computed value. The jq is the transformation language, and the {name: .foo} is the mechanism that constructs the new object, and the map() is the mechanism that transforms the array. The transformation is the reason the jq is not just the query tool but the data-processing tool .
The streaming problem. The jq reads the stream of the JSON documents, and it applies the filter to each document. The --slurp is the mechanism that reads the entire stream into the array and runs the filter once, and the --stream is the mechanism that parses the large input incrementally . The streaming is the mechanism that makes the jq usable for the large files and the pipelines.
The output problem. The default output is the pretty-printed JSON, and the --compact-output is the mechanism that produces the one-line-per-object output, and the --raw-output is the mechanism that writes the string without the quotes . The output control is the mechanism that makes the jq the building block in the shell pipelines, and the pipeline is the reason the jq is the standard.
The security problem. The jq has the history of the CVEs, including the heap buffer overflow, the stack overflow, and the NUL truncation, and the recent releases fixed the vulnerabilities . The security is the reason the tool should be the recent version, and the version is the mechanism that keeps the processing safe.
a. The basic filters and the composition
The . is the identity filter, and it produces the input unchanged. The .foo is the field access, and it produces the value of the foo field, or the null if the field does not exist. The .[] is the iterator, and it produces the each element of the array or the each value of the object as the separate output .
# The identity
echo '{"foo": 42}' | jq '.'
# {"foo": 42}
# The field access
echo '{"foo": 42}' | jq '.foo'
# 42
# The nested field
echo '{"a": {"b": 1}}' | jq '.a.b'
# 1
# The array iterator
echo '[1, 2, 3]' | jq '.[]'
# 1
# 2
# 3
# The object values
echo '{"a": 1, "b": 2}' | jq '.[]'
# 1
# 2
The .foo is the mechanism that selects the field, and the .[] is the mechanism that iterates the collection. The two are the basic selectors, and the selectors are the building blocks.
The | is the pipe, and it feeds the output of the left filter into the right filter. The , is the generator, and it runs both filters on the same input and concatenates the outputs .
# The pipe
echo '{"a": {"b": 1}}' | jq '.a | .b'
# 1
# The comma
echo '{"a": 1, "b": 2}' | jq '.a, .b'
# 1
# 2
The | is the mechanism that composes the filters, and the , is the mechanism that produces the multiple outputs from the same input. The composition is the reason the jq is the language rather than the fixed command.
b. The array transformation and the conditional filtering
The map() applies the filter to each element of the array and returns the new array. The map_values() applies the filter to each value of the object and returns the new object, and it drops the keys whose values produce no output .
# The map
echo '[1, 2, 3]' | jq 'map(. + 1)'
# [2, 3, 4]
# The map_values
echo '{"a": 1, "b": 2}' | jq 'map_values(. + 1)'
# {"a": 2, "b": 3}
The map() is the mechanism that transforms the array, and the map_values() is the mechanism that transforms the object. The two are the counterparts, and the counterparts are the reason the jq handles the both collections.
The select() filters the array by the condition. The function takes the filter expression that returns the boolean, and it produces the input only when the boolean is true.
# The select
echo '[1, 2, 3, 4]' | jq '.[] | select(. > 2)'
# 3
# 4
# The select with the field
echo '[{"name": "a", "age": 30}, {"name": "b", "age": 20}]' | jq '.[] | select(.age > 25) | .name'
# "a"
The select() is the mechanism that filters the array by the condition, and the condition is the mechanism that selects the subset. The filtering is the reason the jq is the query tool, and the query is the mechanism that extracts the relevant data.
The {name: .foo} is the object construction, and it is the mechanism that creates the new object from the existing values.
# The object construction
echo '{"first": "Ada", "last": "Lovelace"}' | jq '{name: .first, surname: .last}'
# {"name": "Ada", "surname": "Lovelace"}
# The shorthand
echo '{"first": "Ada", "last": "Lovelace"}' | jq '{first, last}'
# {"first": "Ada", "last": "Lovelace"}
The {name: .foo} is the mechanism that renames the field, and the {foo} is the mechanism that keeps the field. The construction is the reason the jq is the data-shaping tool, and the shaping is the reason the jq is the standard.
Complete Example Session
# ============================================
# PART 1: THE BASIC FIELD ACCESS
# ============================================
echo '{"name": "Ada", "age": 36}' | jq '.name'
# "Ada"
# ============================================
# PART 2: THE ARRAY ITERATOR
# ============================================
echo '[1, 2, 3]' | jq '.[]'
# 1
# 2
# 3
# ============================================
# PART 3: THE PIPE
# ============================================
echo '{"user": {"name": "Ada"}}' | jq '.user | .name'
# "Ada"
# ============================================
# PART 4: THE MAP
# ============================================
echo '[1, 2, 3]' | jq 'map(. * 2)'
# [2, 4, 6]
# ============================================
# PART 5: THE SELECT
# ============================================
echo '[1, 2, 3, 4]' | jq '.[] | select(. > 2)'
# 3
# 4
# ============================================
# PART 6: THE OBJECT CONSTRUCTION
# ============================================
echo '{"first": "Ada", "last": "Lovelace"}' | jq '{name: .first, surname: .last}'
# {"name": "Ada", "surname": "Lovelace"}
# ============================================
# PART 7: THE COMPACT OUTPUT
# ============================================
echo '{"a": 1, "b": 2}' | jq -c '.'
# {"a":1,"b":2}
# ============================================
# PART 8: THE RAW OUTPUT
# ============================================
echo '{"name": "Ada"}' | jq -r '.name'
# Ada
# ============================================
# PART 9: THE SLURP
# ============================================
echo '{"a": 1}{"a": 2}' | jq -s 'map(.a)'
# [1, 2]
# ============================================
# PART 10: THE CHECKLIST
# ============================================
# [ ] The . is the identity.
# [ ] The .foo is the field access.
# [ ] The .[] is the iterator.
# [ ] The | is the pipe.
# [ ] The map() transforms the array.
# [ ] The select() filters the array.
# [ ] The {} constructs the object.
# [ ] The --compact-output and the --raw-output control the output.
The ten parts covered the basic field access, the array iterator, the pipe, the map, the select, the object construction, the compact output, the raw output, the slurp, and the checklist. The through-line is that the jq is the filter pipeline, and the pipeline is the composition.
Quick Reference
The basic filters
| Filter | Purpose |
|---|---|
. | The identity |
.foo | The field access |
.[] | The array or object iterator |
.[0] | The array index |
.["foo"] | The field access with the special characters |
The composition
| Operator | Purpose |
|---|---|
| ` | ` |
, | The generator |
() | The grouping |
The transformation
| Function | Purpose |
|---|---|
map(f) | Transform the array |
map_values(f) | Transform the object |
select(f) | Filter by the condition |
{name: .foo} | Construct the object |
[.foo, .bar] | Construct the array |
The output flags
| Flag | Purpose |
|---|---|
-c / --compact-output | The one-line output |
-r / --raw-output | The string without the quotes |
-s / --slurp | The entire input as the array |
-S / --sort-keys | The sorted keys |
Best Practices
โ Do This:
# Use the pipe to compose the filters
jq '.user | .name' data.json # โ
# Use the map for the array transformation
jq 'map(.price * 2)' data.json # โ
# Use the raw output for the shell consumption
jq -r '.name' data.json # โ
โ Don’t Do This:
# Use the grep for the JSON parsing
grep '"name"' data.json // the fragile parsing # โ
# Forget the quotes around the filter
jq .name data.json // the shell interprets the .name # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| The filter is interpreted by the shell | The filter is not quoted | Use the single quotes |
| The output has the quotes | The --raw-output is missing | Use the -r flag |
| The multiple documents are not parsed | The --slurp is missing | Use the -s flag |
| The large file is slow | The --stream is missing | Use the --stream flag |
Real-World Examples
1. The field access
jq '.name' data.json
2. The array iterator
jq '.[]' data.json
3. The pipe
jq '.user | .name' data.json
4. The map
jq 'map(.price)' data.json
5. The select
jq '.[] | select(.active)' data.json
6. The object construction
jq '{name: .first, age: .years}' data.json
7. The compact output
jq -c '.' data.json
8. The raw output
jq -r '.name' data.json
9. The slurp
jq -s 'length' data.json
10. The sort keys
jq -S '.' data.json
Visual
The Filter Pipeline
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE FILTER PIPELINE โ
โ โ
โ The JSON input โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The filter .user โ The field access โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The pipe | โ The composition โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The filter .name โ The field access โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The output โ The result โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Iterator
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE ITERATOR โ
โ โ
โ The input: [1, 2, 3] โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The .[] โ The generator โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโผโโโโโโโ โ
โ โผ โผ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1 โโ 2 โโ 3 โ The separate outputs โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Map and the Select
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE MAP AND THE SELECT โ
โ โ
โ The input: [1, 2, 3, 4] โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ map(. * 2) โ The transformation โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ The [2, 4, 6, 8] โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ select(. > 4) โ The filtering โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ The [6, 8] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| The tool | jq |
| The identity | . |
| The field access | .foo |
| The iterator | .[] |
| The pipe | ` |
| The generator | , |
| The transformation | map() and map_values() |
| The filter | select() |
| The output flags | -c, -r, -s, -S |
Key takeaways:
- The
jqis the filter pipeline. The.is the identity, the.foois the field access, the.[]is the iterator, and the|is the pipe. The filters compose, and the composition is the reason thejqis the language rather than the fixed command . - The
map()and themap_values()are the transformation. Themap()transforms the array and returns the array, and themap_values()transforms the object and returns the object. Theselect()is the conditional filter, and the{name: .foo}is the object construction . - The
--slurpreads the entire input as the array. The flag is the mechanism that processes the multiple JSON documents as the single array, and the single array is the reason thejqhandles the streams . - The
--compact-outputand the--raw-outputcontrol the output. The-cis the one-line-per-object output, and the-ris the string without the quotes. The flags are the mechanism that makes thejqthe building block in the shell pipelines . - The recent
jqversions fix the security vulnerabilities. The heap buffer overflow, the stack overflow, and the NUL truncation are the CVEs that the recent releases address, and the recent version is the mechanism that keeps the processing safe .
Remember: The jq is the command-line JSON processor. The . is the identity, the .foo is the field access, the .[] is the iterator, and the | is the pipe. The map() and the select() are the transformation and the filtering, and the --slurp and the --compact-output are the input and the output control. The jq is the standard tool for the JSON in the shell.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!