I have a JSON file that is a mess that I want to prettyprint– what’s the easiest way to do this in python? I know PrettyPrint takes an “object”, which I think can be a file, but I don’t know how to pass a file in– just using the filename doesn’t work.
(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).
Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You’d soon need to write a separate script-file, you could end up with maps whose keys are u”some-key” (python unicode), which makes selecting fields more difficult and doesn’t really go in the direction of pretty-printing.
and you get colors as a bonus (and way easier extendability).
Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the “maximum resident set size” required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.
I once wrote a prettyjson() function to produce nice-looking output. You can grab the implementation from this repo.
The main feature of this function is it tries to keep dict and list items in one line until a certain maxlinelength is reached. This produces fewer lines of JSON, the output looks more compact and easier to read.
print(json){'feed':{'title':'W3Schools Home Page','title_detail':{'type':'text/plain','language':None,'base':'','value':'W3Schools Home Page'},'links':[{'rel':'alternate','type':'text/html','href':'https://www.w3schools.com'}],'link':'https://www.w3schools.com','subtitle':'Free web building tutorials','subtitle_detail':{'type':'text/html','language':None,'base':'','value':'Free web building tutorials'}},'entries':[],'bozo':0,'encoding':'utf-8','version':'rss20','namespaces':{}}
pprint.pprint(json){'bozo':0,'encoding':'utf-8','entries':[],'feed':{'link':'https://www.w3schools.com','links':[{'href':'https://www.w3schools.com','rel':'alternate','type':'text/html'}],'subtitle':'Free web building tutorials','subtitle_detail':{'base':'','language':None,'type':'text/html','value':'Free web building tutorials'},'title':'W3Schools Home Page','title_detail':{'base':'','language':None,'type':'text/plain','value':'W3Schools Home Page'}},'namespaces':{},'version':'rss20'}
import pprint
import json
from urllib.request import urlopen # (Only used to get this example)# Getting a JSON example for this example
r = urlopen("https://mdn.github.io/fetch-examples/fetch-json/products.json")
text = r.read()# To print it
pprint.pprint(json.loads(text))
Here’s a simple example of pretty printing JSON to the console in a nice way in Python, without requiring the JSON to be on your computer as a local file:
import pprint
import json
from urllib.request import urlopen # (Only used to get this example)
# Getting a JSON example for this example
r = urlopen("https://mdn.github.io/fetch-examples/fetch-json/products.json")
text = r.read()
# To print it
pprint.pprint(json.loads(text))