| name | jq-1-8-2 |
| description | jq 1.8.2 — lightweight command-line JSON processor. Use when the user needs to parse, query, transform, or manipulate JSON data from the command line, process API responses, extract fields from JSON, convert between formats (JSON-to-CSV, JSON-to-XML), validate JSON, or work with any structured data in JSON format. Covers filters, builtins, regex, modules, streaming, and all jq 1.8.2 features. |
| metadata | {"tags":["cli","json","data-processing"]} |
jq 1.8.2
Overview
jq is a lightweight, flexible command-line JSON processor written in C with zero runtime dependencies. Think of it as sed/awk/grep for JSON data. Version 1.8.2 includes significant security hardening (CVE fixes for buffer overflows, stack overflow guards, hash collision mitigation) and build improvements (Windows arm64, Docker arm/v7 support).
A jq program is a filter: it takes JSON input and produces JSON output. Filters chain with | (pipe), combine with , (comma), and support variables, conditionals, user-defined functions, regex, modules, and streaming.
Usage
Basic invocation
jq '.' file.json
jq '.name' file.json
jq -c '.[]' file.json
jq -r '.message' file.json
jq --raw-output0 '.path' file.json
jq -j '.id' file.json
echo '{"a":1}' | jq '.a'
jq '.name' file1.json file2.json
jq -f filter.jq data.json
jq --arg name "Alice" '.name == $name' data.json
jq --argjson age 30 '.age > $age' data.json
jq --slurpfile config config.json '. + $config[0]' data.json
jq --rawfile tmpl template.html '$tmpl' null
jq '$ENV.PATH' null
jq -s 'map(.name)' file1.json file2.json
jq -n '{version: "1.0", items: [1,2,3]}'
jq -R 'split(",")' <<< "a,b,c"
jq -e '.valid' file.json
jq -S '.' file.json
jq --tab '.' file.json
jq --indent 4 '.' file.json
jq --stream '[0].name' huge.json
jq --seq '.valid // false' stream.json
jq -C '.' file.json
jq -M '.' file.json
Core filter patterns
jq '.'
jq '.foo'
jq '."foo-bar"'
jq '.["foo-bar"]'
jq '.foo.bar.baz'
jq '.foo?'
jq '.[0]'
jq '.[-1]'
jq '.[2:5]'
jq '.[:3]'
jq '.[-2:]'
jq '.[]'
jq '.items[] | .name'
jq '.. | .name?'
jq '.foo, .bar'
jq '.[] | select(.active == true)'
jq '.[] | select(.age > 18)'
jq '[.[] | select(.type == "user")]'
jq 'map(.name | ascii_upcase)'
jq 'map_values(.price * 0.9)'
jq 'if .status == "ok" then .data else .error end'
jq '.optional // "default"'
jq '.value? // 0'
jq 'try .parse | catch "failed"'
jq '.name as $n | select(.type == $n)'
jq '[.items[] | .price] | add / length'
jq 'reduce .items[] as $item (0; . + $item.price)'
jq 'group_by(.category) | map({cat: .[0].category, count: length})'
jq 'sort_by(.date) | reverse'
jq '"User \(.name) is \(.age) years old"'
jq '.email | test("^[^@]+@[^@]+$")'
jq '.text | gsub("[0-9]"; "*")'
jq '.html | capture("<h1>(?<title>.*)</h1>")'
jq 'def double: . * 2; [items[] | double]'
jq '{name, email}'
jq '{(.key): .value}'
jq '. + {"new_field": 42}'
jq '. * {"nested": {"a": 1}}'
jq 'select(type == "array")'
jq 'map(tostring)'
jq 'map(tonumber)'
Common patterns
jq -R -s 'split("\n") | .[1:] | map(split(",") | {name:.[0], age: (.[1]|tonumber)})' file.csv
jq -r '[.name, .age] | @csv' file.json
jq -r '[.name, .age] | @tsv' file.json
jq '@xml' file.json
jq '.data | @base64'
jq '"hello" | @base64d'
jq '.query | @uri'
jq '"hello%20world" | @uri'
jq '.timestamp | strftime("%Y-%m-%d %H:%M:%S")'
jq -n '{users: [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}'
jq -s '.[0] * .[1]' file1.json file2.json
jq '[.[] | select(.active and .age >= 18 and (.role == "admin" or .role == "editor"))]'
jq '[.items[].tag] | unique'
jq 'group_by(.status) | map({status: .[0].status, count: length})'
jq '.[0:1] | .[0]'
jq '.[-1:] | .[0]'
jq 'nth(5; .[])'
jq 'limit(10; .[])'
jq 'paths(type == "string")'
jq 'getpath(["user", "name"])'
jq 'setpath(["user", "role"]; "admin")'
jq 'del(.metadata.cache)'
jq '.optional // empty'
jq 'map(select(. != null))'
Modules
jq -L ./lib -f program.jq data.json
Gotchas
- Always single-quote jq filters on Unix —
jq '.foo' not jq .foo. The shell interprets ., $, *, [], etc. as special characters. On Windows cmd.exe, use double quotes and escape inner double quotes.
.foo only works for identifier-like keys — keys with hyphens, spaces, or starting with digits need ."key" or .["key"] syntax.
. in a pipeline refers to the current value at that point, not the original input. Use as $var to save the original.
- Pipe is a cartesian product — if left side produces N results and right side produces M per result, you get N×M outputs, not N+M.
select(false) produces no output (not null). Use select(. != null) to filter nulls, or // empty for null/false suppression.
// (alternative) triggers on both null AND false — if you only want null fallback, use if . == null then "default" else . end.
? suppresses all errors, not just missing keys. Use carefully — it can hide real bugs.
map(f) always returns an array; map_values(f) preserves the input type (array stays array, object stays object).
- Numbers lose precision after arithmetic — jq stores literal numbers with original precision but converts to IEEE754 double on any operation. Use
have_decnum to check if your build supports arbitrary-precision decimals.
- Object merge with
+ is shallow — use * for recursive/ deep merge.
group_by requires sorted input — the array must be pre-sorted by the grouping key, or use sort_by(.key) | group_by(.key).
recurse can infinite-loop on circular data. Use recurse(f; condition) to limit depth.
- Regex requires Oniguruma support — check with
jq -n 'have("oniguruma")'. Most prebuilt binaries include it.
inputs reads remaining JSON values from stdin/files after the first one (which is the normal input). Use with -n flag.
--stream outputs [path, value] pairs — you need to reconstruct the structure with reduce/foreach or use fromstream.
- jq 1.8.2 limits array/object size to 2²⁹ elements and path depth for security. Deeply nested structures may hit these limits.
References
- 01-filters-and-operators — Basic filters, operators, types, and values
- 02-builtin-functions — Complete list of built-in functions by category
- 03-conditionals-and-control-flow — if-then-else, try-catch, select, reduce, foreach, while, until
- 04-regex — Regular expression functions: test, match, capture, scan, split, sub, gsub
- 05-advanced-features — Variables, destructuring, def functions, scoping, generators, assignment operators
- 06-io-and-streaming — input/inputs, debug, stderr, streaming parse, fromstream/tostream
- 07-modules — import, include, module declarations, library paths, modulemeta
- 08-cli-options — Complete command-line option reference with examples
- 09-formats-and-escaping — @csv, @tsv, @sh, @base64, @html, @json, @text, @uri, @xml, string interpolation
- 10-dates-and-math — Date/time functions, math library (sin, cos, exp, log, etc.)