Tuesday, 27 August 2024

Pretty-Printing JSON in Shell Scripts

Working with JSON data directly in the command line can be cumbersome due to its compact and hard-to-read format. Fortunately, there are several tools and techniques you can use within a Unix shell to pretty-print JSON data, making it easier to read and debug. In this post, we’ll explore different methods to format JSON using various command-line tools.

Using jq

jq is a lightweight and flexible command-line JSON processor. It can pretty-print JSON with minimal effort:

echo '{"foo": "lorem", "bar": "ipsum"}' | jq .

This command takes a JSON string as input and pretty-prints it with proper indentations and color-coding (if your terminal supports colors).

Read more »

Labels: