JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is widely used for exchanging data between web servers, web applications, and mobile apps. However, JSON data can be difficult to read and understand when it’s not formatted properly. In this tutorial, we’ll explore how to pretty print JSON data in shell scripts using various tools and techniques.
Introduction to Pretty Printing JSON
Pretty printing JSON involves formatting the data with indentation, line breaks, and spacing to make it more human-readable. This can be particularly useful when working with large or complex JSON datasets.
Using Python’s json.tool Module
One of the most straightforward ways to pretty print JSON data in a shell script is by using Python’s built-in json.tool
module. Here’s an example:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
This will output the following formatted JSON:
{
"foo": "lorem",
"bar": "ipsum"
}
You can also use json.tool
with a file containing JSON data:
python -m json.tool my_json.json
Or, if you’re retrieving JSON data from an API, you can pipe the output to json.tool
:
curl http://my_url/ | python -m json.tool
For convenience, you can create an alias for the json.tool
command:
alias prettyjson='python -m json.tool'
Using jq
Another popular tool for working with JSON data is jq
. Here’s an example of how to use jq
to pretty print JSON:
echo '{"foo": "lorem", "bar": "ipsum"}' | jq .
This will output the following formatted JSON:
{
"bar": "ipsum",
"foo": "lorem"
}
You can also use jq
with a file containing JSON data:
jq . my_json.json
Or, if you’re retrieving JSON data from an API, you can pipe the output to jq
:
curl http://my_url/ | jq .
Using Node.js and JSON.stringify
If you have Node.js installed on your system, you can use the JSON.stringify()
method to pretty print JSON data. Here’s an example:
console.log(JSON.stringify({ "foo": "lorem", "bar": "ipsum" }, null, 4));
This will output the following formatted JSON:
{
"foo": "lorem",
"bar": "ipsum"
}
You can also use JSON.stringify()
with a file containing JSON data:
const fs = require('fs');
const json = JSON.parse(fs.readFileSync('my_json.json'));
console.log(JSON.stringify(json, null, 4));
Using underscore-cli
Another tool that offers advanced pretty printing capabilities is underscore-cli
. Here’s an example of how to use underscore-cli
to pretty print JSON:
underscore -i data.json print
This will output the following formatted JSON:
{
"foo": "lorem",
"bar": "ipsum"
}
You can also pipe JSON data to underscore-cli
:
echo '{"foo": "lorem", "bar": "ipsum"}' | underscore print
Conclusion
In this tutorial, we’ve explored several ways to pretty print JSON data in shell scripts using Python’s json.tool
module, jq
, Node.js and JSON.stringify()
, and underscore-cli
. Each of these tools offers its own strengths and weaknesses, and the choice of which one to use will depend on your specific needs and preferences. By using these tools, you can make working with JSON data easier and more efficient.