pretty-print json
Show JSON indented and coloured, pick values out of it, from a file or a web request.
Steps
-
jq . file.jsonthe file, indented and coloured
-
curl -s https://api.example.com/items | jq .a web answer, the same way
-
jq '.items[0].name' file.jsonone value out of it
-
jq -r '.items[].name' file.jsona value from every item, as plain text
-
python3 -m json.tool file.jsonwithout jq: indented by Python
Notes
- jq is a package of its own on most systems:
sudo apt install jq,brew install jq.
Commands used
jqcurlpython3
Each has a page in lore: lore jq opens it.
Related recipes
- count lines of codeHow many lines of code a project has, in total and by language.
- fix windows line endings"bad interpreter: ^M" and odd characters: the file has Windows line endings (CRLF). Find them and change them to Unix ones (LF).
- read a file line by lineGo through a file's lines in a script, also split into fields, without losing spaces or backslashes.
- add up numbers in a fileSum a column of numbers, from a file or a command's output.