Make Your Logged JSON Readable

I am tired of logging JSON and seeing [Object] or [Array] on my terminal. Screenshot 2020-09-15 at 4.46.12 PM.png

So I set out on a mission to do something about it. Enter jq, a lightweight and flexible command-line JSON processor. You can follow the installation guide in the docs for your OS.

Now all you have to do is console.log(JSON.stringify(data)) and pipe the command you run to jq. Like shown below:

node index.js | jq

Which will output this beautiful JSON ✨ that we wanted in the first place. Screenshot 2020-09-15 at 5.00.08 PM.png

Sometimes the output isn't always just stringified JSON, there can also be other logs and errors in some cases. For jq to function properly it needs to ignore those logs. You can ignore it by using

 node index.js | jq -R 'fromjson? | select(type == "object")'

Hope you found the article useful. Let me know your thoughts in the comments below or you can @ me on twitter

Bolaji Ayodeji's photo

Well written Vamsi Rao, thanks for sharing!

Chris Bongers's photo

This is amazing! Thank you for sharing

Mohd Shad Mirza's photo

😮

Sandeep Panda's photo

Nice - Bookmarked -- going to try out soon!

Syed Fazle Rahman's photo

This is great! 👏

Vamsi Rao's photo

Thank you :)

Edgar John's photo

Quick and useful tip, thanks for sharing