Structured JSON with jq
JSON is a structured data format with strings, numbers, booleans, null, arrays and objects. Bash variables hold strings, not JSON trees. Use jq as a separate executable to parse and transform JSON, rather than regular expressions or shell word splitting.
Extraction is not schema validation
jq -r '.name' prints an unquoted string, but null becomes the text null; a missing key also commonly evaluates to null. jq -e bases its exit status on the last output: truthy (including zero or empty string) succeeds, false or null fails, and no valid result also has a distinct failure code. A valid boolean false may be legitimate data, so use an explicit type or presence predicate when validating. Malformed JSON causes a parse error. For a one-object response contract, consider slurping and checking that exactly one JSON value exists; a filter otherwise processes each top-level value in the stream.
Use --arg to pass external strings as data to a fixed filter, and --argjson only for valid JSON values. Keep filters single quoted where possible. For a whole array of arbitrary strings, retain JSON or use jq 1.7's --raw-output0 with NUL-aware readers; never split raw lines if strings may contain newlines. Bash cannot store embedded NUL bytes. Capture command status before using output, and do not rely on a process substitution to propagate jq failure. Examples use jq 1.7. For the fixed name fixture, command substitution strips trailing newlines; it does not preserve arbitrary strings byte-for-byte. The false-value drill must check jq status 1 specifically rather than accepting a parse error as false. NUL framing is suitable only for strings without embedded NUL; jq --raw-output0 rejects an output string containing NUL. Keep the JSON representation for such values.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. HTTP examples require the included lab-fixtures/v25/mock_api.py server: run python3 on that file in a separate terminal, export BM_API_BASE with its printed loopback URL, and stop the server with Ctrl-C when done. Use a fresh server for each HTTP example. curl and jq are external dependencies.
Extract and preserve a string
printf '%s\n' '{"name":"daily report"}' > item.json || exit 1name=$(jq -er '.name | strings' item.json) || exit 1printf '[%s]\n' "$name"Expected stdout:
[daily report]The string is preserved as one argument. This fixed fixture is a single object; general validation must enforce that contract.
Distinguish false, null and missing
printf '%s\n' '{"enabled":false,"label":null}' > item.json || exit 1jq -r 'has("enabled"), has("missing"), (.label == null)' item.json || exit 1jq -e '.enabled' item.json >/dev/nullrc=$?[[ $rc == 1 ]] || exit 1printf 'value-status=%s\n' "$rc"jq -e 'has("enabled") and (.enabled | type == "boolean")' item.jsonExpected stdout:
truefalsetruevalue-status=1trueThe false value exists and has the correct type, even though -e on the value alone fails.
Pass a string as data
Expected stdout:
{"name":"Ada \"A\""}jq handles the JSON escaping. -n constructs a value without input; -c emits compact JSON.
Common mistakes
Raw output does not validate a schema; jq -e .flag rejects a valid false. Do not splice user data into the filter source.
Practice
Add a missing name, null, false, and a name containing a space or newline to a fixture. Design a type check that accepts only string names and preserves their contents.
Next: Pagination and API data handling