Text and processing
The jq command
Reads, filters and formats JSON from the terminal, with no script needed.
Most useful options
-rWrites strings without quotes, ready to store in a shell variable-cPrints each value on a single line, without indentation-sReads all inputs and joins them into a single array before applying the filter-nDoes not read input: the filter starts with null, useful for building JSON from scratch-eChanges the exit status: 0 if the last value is neither false nor null, 1 if it is--arg nombre valorDefines the variable $name as a string inside the filterCopy-ready examples
$ echo '{"a":1}' | jq .aextracts field a from a JSON value.Example output
1
$ echo '[{"n":"ana","edad":31},{"n":"luis","edad":27}]' | jq -r '.[] | .n + ": " + (.edad | tostring)'walks an array and writes one line per element.Example output
ana: 31 luis: 27
$ echo '{"a":[1,2,3]}' | jq -c '.a | length'counts the elements of an array.Example output
3
$ echo '{"edad":3} {"edad":4}' | jq -s 'map(.edad) | add'joins several objects and adds up a field.Example output
7
$ jq -n --arg nombre Miguel '{saludo: ("Hola " + $nombre)}'builds a new JSON value using a shell variable.Example output
{"saludo":"Hola Miguel"}You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| jq: parse error: Invalid numeric literal at line 1, column 4 | The input is not valid JSON. Here there is an unquoted value or a stray comma, and jq stops where it cannot keep reading. | Check the file with cat and fix the syntax. The column number shows where the problem started. |
| jq: Unknown option --zzz | jq does not recognize that option. It is often a typo or a flag from another tool. | Check jq --help and use the options from the list, such as -r or -c. |
Typical mistakes
- Malformed JSON gives a parse error: check it first with cat or with jq empty file.json.
- Without -r, strings come out in quotes, as in JSON. If you store them in a shell variable, use -r.
- Put the filter in single quotes so the shell does not interpret $, parentheses or backslashes.
- Very large integers can lose digits depending on the jq version: keep them as strings if you need exact values.
Differences from similar commands
python3 -m json.tool: formats JSON, but does not filter or extract fields. It works when you only need to read it indented.- grep: searches text in lines without understanding the structure. It does not know which field is which.
yq: does something similar with YAML, and its filter syntax looks quite like jq's.
FAQ
How do I get a field's value without quotes?
With jq -r '.field'. Without -r, jq prints the string in quotes, as it would in JSON.
How can a script check that a JSON file is valid?
Run jq empty file.json: it prints nothing and exits with 0 when the file is valid. If it is not, it shows an error and exits with a non-zero code.
Why does a big number come out rounded?
Many jq versions work with double-precision numbers, so very large integers lose digits. Keep them as strings if you need the exact value.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course