Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Process JSON at the shell using jq

📅 2017-Feb-23 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ jq, json ⬩ 📚 Archive

jq is an awesome tool to play with JSON data at the commandline. It is akin to the Unix text manipulation tools, but for JSON.

$ sudo apt install jq
$ cat ugly.json | jq '.'

What this command is doing is piping the JSON file to jq and then asking it to filter everything, but jq formats everything it touches, so we get pretty JSON formatting out from it.

I have found this useful to format huge JSON files. jq is much faster at this job compared to the json.tool in Python.

$ cat foobar.json | jq '.["records"][6]["name"]'

Note the use of single quotes to encapsulate the expression and use of double quotes inside to specify keys in dictionaries.

$ cat foobar.json | jq '.[] | [.name, .time, .percent] | @csv' > foobar.csv

References

Tried with: jq 1.5 and Ubuntu 16.04


© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email