How to pretty print JSON on Linux terminal


#1

Problem: I have a JSON file in my Linux server which is not formatted correctly. I want to pretty print that JSON file.

Solution: You can use a simple python utility for this.

For example, if you have a JSON file named example.json and it is formatted as shown below.

{ "name":"devopscube", "domain":"discuss.devopscube.com", "type":"community" }

Now you want to pretty print it. You can use the following command to do that.

cat variables.json | python -m json.tool

Now the output should look like the following.

{
"domain": "discuss.devopscube.com",
"name": "devopscube",
"type": "community"
}

If you got any other tips, reply in this thread.

And, if you want to save the pretty print to another file, you can use the following command.

cat example.json | python -m json.tool | tee pretty-output.json