Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pretty-print a JSON file from the command line?

I've a file with a sequence of JSON element:

{ element0: "lorem", value0: "ipsum" } { element1: "lorem", value0: "ipsum" } ... { elementN: "lorem", value0: "ipsum" } 

Is there a shell script to format JSON to display file content in a readable form?

I've seen this post, and I think is a good starting point!

My idea is to iterate rows in the file and then:

while read row; do echo ${row} | python -mjson.tool; done < "file_name" 

Does anyone have any other ideas?

like image 854
Luca Davanzo Avatar asked Nov 28 '13 11:11

Luca Davanzo


People also ask

What is pretty print JSON?

Pretty printing is a form of stylistic formatting including indentation and colouring. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and for machines to parse and generate. The official Internet media type for JSON is application/json .

How do I print JSON beautify in Python?

Write Pretty Print JSON data to file To write a Python object as JSON formatted data into a file, json. dump() method is used. Like json. dumps() method, it has the indents and separator parameters to write beautified JSON.


1 Answers

Pipe the results from the file into the python json tool 2.6 onwards

python -m json.tool < 'file_name' 
like image 125
Shawn Vader Avatar answered Sep 20 '22 05:09

Shawn Vader