jq Foundations
JSON query language: filter streams, projection, selection, null/presence, data parameters, grouping, aggregation, updates and explicit validation. This is beyond parsing JSON; no remote data or schema conformance claim.
Audience: Learners comfortable saving plain text and invoking an installed command-line tool.
Practice: 4 to 6 hours.
Runtime and boundaries: Export the fixed .jq program and companion input. /usr/bin/jq --version reports jq-1.7.1-apple; the upstream 1.7 manual is context, not proof of every Apple-build behavior. -c makes JSON output compact. -r would remove JSON quoting from string outputs and is not used here. Programs are trusted and inputs tiny. Use --arg or --argjson for data, never concatenate data into program text. No modules, environment access, external files or network commands are used. The HTML only exports text. -e result statuses and deliberate compile/runtime errors are separate outcomes. Numeric examples use small finite integers; exact large-integer or financial arithmetic is outside scope.
Lessons
A filter may produce many results
A jq filter consumes a JSON value and can emit zero, one or many values. .items[] emits each array element; enclosing that stream in brackets collects it. A pipe feeds every result to the next filter. Know whether the intended output is one array or several JSON texts.
Choose fields and keep their types
Object projection constructs a new object. Accessing an absent object member produces null, which differs from a false value or zero. has tests whether a member exists. A field lookup alone does not validate the shape of its parent object.
Selection follows a contract
select keeps or discards the current value; map runs its filter for each array element and collects every emitted result. The selection and grouping examples assume correctly typed records. Comparisons can order values of different types, so a threshold comparison alone is not numeric validation.
Missing, null and false need a policy
The // fallback treats both null and false as absent results. It does not replace zero or an empty string. Use an explicit condition when false must be preserved, and has when missing must be distinguished from a present null.
Pass data without constructing source
Keep the filter in a .jq file. --arg binds a string; --argjson parses a JSON value. They do not splice values into jq syntax. Shell quoting remains a separate layer. Malformed --argjson data is a command failure, not a filter compile error.
Aggregation and order are deliberate
-s collects all input JSON texts into one array, so it is bounded here rather than advertised as a large-data technique. group_by sorts groups by the grouping key. reduce carries an explicit accumulator through a stream; duplicate keys need an intentional merge policy.
Validate before calculation
The bounded total accepts an array of at most ten integral numeric values from zero through 120. It rejects strings, booleans and out-of-range values using error. An empty array has total zero. This small contract is not JSON Schema validation.
Results and status are separate evidence
Without -e, no output can still be successful. With -e, false/null as the last result returns status 1 and no result returns 4. A compiler rejection and a runtime error are different. Test bytes, result count and status; never treat successful parsing as permission to execute arbitrary code.
Reference cards
Identity: .
Return the current input.
Field: .task.name
Read an object member along a path.
Iteration: .items[]
Emit each array element as a separate result.
Collection: [filter]
Collect all results into one array.
Projection: {name, minutes}
Construct an object from selected members.
Selection: select(condition)
Keep the current value only when the condition is truthy.
Mapping: map(filter)
Apply a filter across an array.
Presence: has("enabled")
Test whether an object member exists.
Fallback: value // default
Fallback for null or false, not for zero.
Parameters: --arg / --argjson
Pass a string or parsed JSON data value.
Input collection: -s
Read all input JSON texts into one array.
Grouping: group_by(.kind)
Group and order by the key.
Reduction: reduce stream as $x (init; step)
Accumulate a stream with explicit state.
Update: path |= filter
Return a value with the selected path transformed.
Contract: type / floor / all / error
Check the small accepted domain and reject violations.
Status: -e
Make false/null or no-result outcomes nonzero.
Supporting configuration: Run instructions.txt
Export the fixed .jq program and companion input. /usr/bin/jq --version reports jq-1.7.1-apple; the upstream 1.7 manual is context, not proof of every Apple-build behavior. -c makes JSON output compact. -r would remove JSON quoting from string outputs and is not used here. Programs are trusted and inputs tiny. Use --arg or --argjson for data, never concatenate data into program text. No modules, environment access, external files or network commands are used. The HTML only exports text. -e result statuses and deliberate compile/runtime errors are separate outcomes. Numeric examples use small finite integers; exact large-integer or financial arithmetic is outside scope.
Examples and practice
Code below is reading material, not an execution console. Download the edition for the complete example files and companion modules.
jq 01 - Project typed fields
Projection retains the numeric type. A missing object member yields null.
{name: .task.name, minutes: .task.minutes, missing: .absent}
Expected:
{"name":"read","minutes":20,"missing":null}Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 01 Project typed fields.jq' < 'jq 01 Project typed fields Input.json'
Companion file: jq 01 Project typed fields Input.json
{"task":{"name":"read","minutes":20}}
Intentionally invalid jq: expected jq compiler diagnostic containing compile error. Expected rejection; not a successful program. No runtime execution follows rejection.
.task |
jq 02 - Streams and arrays
This produces two JSON string results, not one array. The companion must contain an items array.
.items[] | .name
Expected:
"read" "code"
Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 02 Streams and arrays.jq' < 'jq 02 Streams and arrays Input.json'
Companion file: jq 02 Streams and arrays Input.json
{"items":[{"name":"read"},{"name":"code"}]}jq 03 - Select records
The records already satisfy a small schema: name is text and minutes a small number. Selection is not a schema validator.
[.items[] | select(.minutes >= 20) | {name, minutes}]
Expected:
[{"name":"read","minutes":20}]Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 03 Select records.jq' < 'jq 03 Select records Input.json'
Companion file: jq 03 Select records Input.json
{"items":[{"name":"read","minutes":20},{"name":"rest","minutes":5}]}jq 04 - False null and missing
The fallback replaces false. The explicit condition preserves it; has distinguishes a missing member from a present null.
{fallback: (.enabled // true), present: has("enabled"), preserved: (if .enabled == null then true else .enabled end)}
Expected:
{"fallback":true,"present":true,"preserved":false}Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 04 False null and missing.jq' < 'jq 04 False null and missing Input.json'
Companion file: jq 04 False null and missing Input.json
{"enabled":false}jq 05 - Parameters remain data
--arg keeps punctuation in a string. --argjson passes a numeric threshold. The fixed filter is never constructed from either value.
{label: $label, names: [.items[] | select(.minutes >= $minimum) | .name]}
Expected:
{"label":"Study & learn","names":["read"]}Run with the supplied authored input:
/usr/bin/jq -c --arg label 'Study & learn' --argjson minimum 20 -f 'jq 05 Parameters remain data.jq' < 'jq 05 Parameters remain data Input.json'
Companion file: jq 05 Parameters remain data Input.json
{"items":[{"name":"read","minutes":20},{"name":"rest","minutes":5}]}Intentionally invalid jq: expected jq compiler diagnostic containing compile error. Expected rejection; not a successful program. No runtime execution follows rejection.
def broken(: .;
jq 06 - Collect a tiny JSON stream
-s collects two JSON texts before evaluation. The sum assumes numeric minutes; an empty stream produces an empty array and total zero.
{count: length, total: (map(.minutes) | add // 0)}
Expected:
{"count":2,"total":60}Run with the supplied authored input:
/usr/bin/jq -c -s -f 'jq 06 Collect a tiny JSON stream.jq' < 'jq 06 Collect a tiny JSON stream Input.json'
Companion file: jq 06 Collect a tiny JSON stream Input.json
{"minutes":20}
{"minutes":40}
jq 07 - Group and aggregate
Groups are ordered by kind, so break comes first even though work appeared first in the input. The records are already typed.
.items | group_by(.kind) | map({kind: .[0].kind, total: (map(.minutes) | add)})
Expected:
[{"kind":"break","total":5},{"kind":"work","total":60}]Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 07 Group and aggregate.jq' < 'jq 07 Group and aggregate Input.json'
Companion file: jq 07 Group and aggregate Input.json
{"items":[{"kind":"work","minutes":20},{"kind":"break","minutes":5},{"kind":"work","minutes":40}]}jq 08 - Validate a bounded total
Only at most ten integral numbers from zero through 120 are accepted. Boolean, string and nonintegral numeric inputs are rejected before a total is returned.
def valid: type == "number" and . >= 0 and . <= 120 and floor == .;
if type != "array" then error("expected array")
elif length > 10 then error("too many values")
elif all(.[]; valid) then {total: (add // 0)}
else error("invalid value") end
Expected:
{"total":60}Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 08 Validate a bounded total.jq' < 'jq 08 Validate a bounded total Input.json'
Companion file: jq 08 Validate a bounded total Input.json
[20,40]
Intentionally invalid jq: expected jq compiler diagnostic containing compile error. Expected rejection; not a successful program. No runtime execution follows rejection.
[1,
jq 09 - Return an updated value
The output adds or replaces done for each task. jq does not rewrite the input file; fields outside the selected path are retained.
.tasks |= map(. + {done: (.minutes == 0)})
Expected:
{"tasks":[{"name":"read","minutes":20,"done":false},{"name":"rest","minutes":0,"done":true}]}Run with the supplied authored input:
/usr/bin/jq -c -f 'jq 09 Return an updated value.jq' < 'jq 09 Return an updated value Input.json'
Companion file: jq 09 Return an updated value Input.json
{"tasks":[{"name":"read","minutes":20},{"name":"rest","minutes":0}]}jq exercise A - Pending names
Only an explicit false value is selected; a missing done field becomes null and does not match. Sorting is deliberate.
[.items[] | select(.done == false) | .name] | sort
Expected:
["read","write"]
Run with the supplied authored input:
/usr/bin/jq -c -f 'jq exercise A Pending names.jq' < 'jq exercise A Pending names Input.json'
Companion file: jq exercise A Pending names Input.json
{"items":[{"name":"write","done":false},{"name":"read","done":false},{"name":"rest","done":true}]}Practice: Return an array of names for explicitly unfinished items, in sorted order.
jq exercise B - Merge duplicate names
The object accumulator deliberately sums duplicate names. These fixed records have string names and small numeric minutes.
reduce .[] as $row ({}; .[$row.name] = ((.[$row.name] // 0) + $row.minutes))
Expected:
{"read":60,"rest":5}Run with the supplied authored input:
/usr/bin/jq -c -f 'jq exercise B Merge duplicate names.jq' < 'jq exercise B Merge duplicate names Input.json'
Companion file: jq exercise B Merge duplicate names Input.json
[{"name":"read","minutes":20},{"name":"read","minutes":40},{"name":"rest","minutes":5}]Intentionally invalid jq: expected jq compiler diagnostic containing not defined. Expected rejection; not a successful program. No runtime execution follows rejection.
not_a_defined_function
Practice: Aggregate minutes by name without silently replacing an earlier value.
jq exercise C - Four observable checks
The program compares all four results with an explicit expectation and raises an error on a mismatch. This is a toy test, not a testing framework.
def valid: type == "number" and . >= 0 and . <= 120 and floor == .;
[0,120,121,"12x"] | map(valid) as $actual
| if $actual == [true,true,false,false] then {checks:4, status:"passed"}
else error("test failed") end
Expected:
{"checks":4,"status":"passed"}Run with the supplied authored input:
/usr/bin/jq -c -n -f 'jq exercise C Four observable checks.jq' < /dev/null
Practice: Check zero, the upper boundary, an out-of-range value and numeric-prefix text.
Prompt templates
These are editable planning prompts in the downloadable workbench; they do not contact an AI service here.
Build a structured transform
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Provide a fixed program, separate authored input, exact command and expected serialized output. Separate predicted behavior from executed evidence.Debug an unexpected result
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Reduce the input and distinguish parse errors, missing matches, wrong types, namespaces and process status. Separate predicted behavior from executed evidence.Test boundaries and contracts
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Include empty input, missing values, false/zero, escaping and rejected types; specify stdout, stderr and exit status. Separate predicted behavior from executed evidence.Refactor with a regression contract
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Preserve result count, order, types, serialization and failure policy; explain the change. Separate predicted behavior from executed evidence.Document the transformation
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. State schema assumptions, supported tool version, numeric limits, encoding, resource bounds and side effects. Separate predicted behavior from executed evidence.Explain with an exercise
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Trace one input through the transform and provide a separate exercise and solution. Separate predicted behavior from executed evidence.Further reading
- jq 1.7 manual — Filter semantics, data parameters, input/output options and exit statuses.
- Installed jq manual — Installed Apple jq 1.7.1 command-line behavior; executed examples remain authoritative.
Download the offline edition
Complete Atlas 0.11.0 ZIP (6,079,970 bytes) · 411-page guide and catalogue PDF · Versioned source JSON
Unzip the edition and open Programming and Scripting Atlas.html, Web Foundations.html, Python Foundations.html, SQL Foundations.html, Shell Foundations.html, TypeScript Foundations.html Data Formats Foundations.html, C Foundations.html C++ Foundations.html, Ruby Foundations.html, Perl Foundations.html, Swift Foundations.html, Makefile Foundations.html, awk Foundations.html, sed Foundations.html, jq Foundations.html, XSLT 1.0 Foundations.html, Tcl Foundations.html, Expect Foundations.html or Objective-C Foundations.html locally. The original interactive workbenches retain their offline prompt-building, local presets and export controls. Read the included README before running examples yourself. The workbench does not execute pasted code or call an AI service; prompt text is for you to inspect and use separately.
Version 0.11.0 is pinned here. The other 257 registry entries remain planned; this edition does not claim complete language coverage.
Evidence and limits
Objective-C adds 12 accepted native programs, four separate compiler or warning-policy rejections, 120 root checks and 147 independent native checks. Two rejection fixtures are warnings promoted by -Werror; they are not intrinsic syntax failures. No rejected fixture was executed. Tests dated 26 October 2026 used Apple Clang 21.0.0, ARC, C17 base mode, macOS SDK 27.0 headers and system Foundation 6.9 (5027.0.69) on macOS 27.0.1 arm64. This is introductory macOS/Foundation coverage, not iOS, GNUstep, Objective-C++, UI, concurrency or cross-platform coverage. No sanitizers, leak detector, race analysis, allocation/disk-full/permission fault injection or asynchronous timing tests were run. Trusted authored fixtures and process bounds do not establish an arbitrary-code sandbox.
The pinned Atlas 0.11.0 evidence records 607 browser checks, 55 Python harness checks plus four unittest methods, 74 SQL checks, 515 shell checks across 71 runs, 99 TypeScript checks, 51 Data Formats checks, 119 C checks, 116 C++ checks, 97 Ruby checks, 97 Perl checks, 105 Swift harness checks and 97 Makefile harness checks, 145 awk checks, 145 sed checks, 161 jq checks, 157 XSLT checks, 150 Tcl checks and 161 Expect checks. These are separate measures. Tcl and Expect tests are dated 26 October 2026 on installed Tcl 8.5.9 / Expect 5.45 with Tcl 8.5.9 on macOS arm64. These old versions are recorded teaching evidence, not deployment recommendations. Tcl has 12 accepted programs and Expect has 12 accepted controllers; each has four expected evaluation failures, not static compilation passes. Tcl has nine scenarios, five expected nonzero; Expect has seven scenarios, four expected nonzero. Expect default controllers invoke 15 toy children, including two expected child failures; one scenario also expects a failed child. Controller success does not mean every child succeeds. The authoring release records 100 owned-runner regression checks and 109 independent runner checks separately. Only authored fixtures and retained native child handles are covered. Cooperative cleanup requires event-loop progress; outer-timeout child cleanup remains uncertain without complete wait receipts. The retired PID-inspection branch was not retried. No sign-ins, secrets, network or external application control are taught or tested by this pack. awk and sed are text-processing languages; their examples use fixed authored ASCII input and the C locale with installed awk 20200816 and macOS sed on one platform. jq and XSLT each have 12 accepted examples and four separately rejected programs. jq has 21 scenarios, including five contract errors and one no-result status; XSLT has 20 scenarios, including five terminating messages and one CLI parameter rejection. Expected nonzero outcomes are not successful programs. Tests dated 26 October 2026 used jq 1.7.1-apple and XSLT 1.0 with libxslt 1.1.35/libxml2 2.9.13, fixed trusted programs and tiny authored inputs in the C locale on macOS arm64. These old installed XML libraries are recorded learning environments, not recommendations for untrusted-document processing. No imports, external documents, DTDs, custom/external entities, extension functions or generated source are covered. Command flags and bounds are not a complete sandbox. Standalone XPath remains planned; supporting XPath 1.0 expressions are limited to these XSLT examples. These tests do not establish GNU-tool equivalence, Unicode behavior, arbitrary-script safety or cross-platform portability. Swift has 12 accepted programs and four expected static rejections; Makefile has 12 accepted default targets, four expected parser rejections, eight additional scenarios and one expected recipe failure. Makefile is build scripting, not a general-purpose language. The Swift and Makefile test execution dates remain 25 October 2026. awk and sed tests are dated 26 October 2026. Delivery verification does not replace or refresh test execution dates. TypeScript includes 12 accepted programs executed as JavaScript and nine intentionally invalid cases correctly rejected. Each C/C++ pack has 12 accepted programs run normally and with AddressSanitizer/UndefinedBehaviorSanitizer, plus four expected compiler rejections that are never executed. Ruby and Perl each have 12 syntax-accepted programs executed and four expected syntax rejections; rejected cases are not successful programs. Syntax checks may execute compile-time code and are not arbitrary-code sandboxes. Sanitizer results cover executed paths only; leak detection and allocation-failure injection were not tested and memory safety is not proved.
Tested environments are Chrome 155, CPython 3.9.6, SQLite CLI 3.54.0, macOS sh/Bash 3.2.57/Zsh 5.9, TypeScript 6.0.3, Node 24.18.0 Apple Clang 21.0.0, Ruby 2.6.10p210 and Perl 5.34.1, Apple Swift 6.4 in Swift 6 mode and GNU Make 3.81 on one arm64 macOS host. C uses a C17 subset; C++ uses a C++20 subset with libc++ 220106. Python 3.9.6 is unsupported upstream. Ruby 2.6 is end-of-life upstream; the recorded installed interpreter is not a recommended modern runtime. Data Formats uses installed Tomli 1.0.3 through a private fallback; the Python tomllib branch was not run. No new-installation recommendation, complete language/format conformance, schema certification, hardened untrusted XML parsing or general-purpose execution sandbox is established. Other platforms, browsers and screen readers remain unverified. The 145 official references belong to the saved Atlas review, not fresh website-time verification.