Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:
>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
To parse a file, use json.load():
with open('filename.txt', 'r') as handle:
parsed = json.load(handle)
Answer from Blender on Stack OverflowJson, c#, pretty printing?
Pretty print JSON from URL
how to have my view output pretty printed json
Pretty Print JSON Response
As long as the MIME type is set to application/json on the server response, it shouldn't matter what the URL looks like; it should get pretty printed in the built-in developer tools as far as I can remember.
More on reddit.comVideos
Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:
>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
To parse a file, use json.load():
with open('filename.txt', 'r') as handle:
parsed = json.load(handle)
You can do this on the command line:
python3 -m json.tool some.json
(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.
You can also use jq:
jq . some.json
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.
» npm install pretty-print-json
Hey peeps, I have seen a few ways to work with json output and was curious what is the “best” or most effective way to take in and use json. It seems silly to have to make a class to parse json data, but if that’s the best way then fine, I can do that. The next thing is what might be the best way to display the json output? Python has something called pretty printing that tabs the data out in a clean format, I have also seen a text tree. Thanks in advance :)
JSON Formatter provides buttons to toggle between raw and formatted views of a JSON document.
It is open source. As of March 2023, the chrome extension was last updated on January 2023 and the last commit was in March 2023.
As of November 2016, the last commit was in March 2015. There are over 100 forks, many of which have more recent commits. It's not immediately clear if one of those forks is better maintained.
The OP of this answer originally wrote: "I feel like a big dummy. Some more thorough searching turned up https://chrome.google.com/webstore/detail/the-missing-json-inspecto/hhffklcokfpbcajebmnpijpkaeadlgfn/related, which is exactly what I need." However, an edited version of this answer indicated that that extension was not open source and was no longer maintained.
Here is a way with Chrome 97 and no plugins.
- Visit a API or JSON resource in the URL
- Open developer tools. (F12)
- Click the Source tab.
- Open a source or hit CTRL-P
- Select the JSON
- In the top, select Pretty Print
- You get nice JSON human-readable formatting!
Here is an example:


Hello Everyone,
I've hit a frustrating wall and after much googling I took a deep breath and felt the best approach was to ask you all for advice.
I'm making a very simple script and need assistance with what tool I should use to move forward.
Step 1: Fetch the URL Step 2: Make the JSON pretty. Such as a top-down listing of Title and URL only.
import requests
#make the URL call
page = requests.get('http://gleamlist.com:5000/api')
#verify the page has content - just for testing.
print(page.content)I've tried using the json module but confuse myself and I've thought maybe BS4 would help but also confuse myself. I believe JSON is basically a dictionary and I want to parse out the info. Could anyone assist me with some tips or a specific topic to research and implement please?
Try this:
- Open the Chrome DevTools (Ctrl+Shift+I).
- Navigate to the Overrides section in the Sources tab.
- Select the folder to save overrides (Chrome will request permissions for that).
- In the Network tab, go to the JSON file.
- Right-click the JSON response and select Save for overrides.
- Edit the saved JSON file somehow for the expected formatting.
- Refresh the page, and Chrome should load the locally formatted version.
As an alternative approach Use tools like mitmproxy or Charles Proxy to intercept JSON responses. Configure the proxy to modify JSON responses and then route Chrome/Brave traffic through the proxy.
How can I change a browser setting to make the "Pretty-print" checkbox checked by default without installing extensions?
You may change your web browser and luckily find another browser which has that setting enabled by default. Alternatively you may use some open source browser like Mozilla Firefox and change the default settings as you wish.
Why would anyone not want Pretty-print enabled?
JSON is not a format for human beings, but primarily for machines.
There is still chance that you (as a human) need to parse some JSON response, may be to debug a server-side script code or any other reason, but that chance is little.
The main use case for a human to use a web browser to get a JSON response, is to save it as a file and pipe it into another application or process.
Here the file size matters the most and thus it is not pretty-printed by default. Keep in mind that a JSON file may have thousands of entries and does not always have only multiple entries.
Why wouldn't it at least be enabled by default?
As said, because the file size matters the most and JSON response may have thousands of entries.
Update
Also, processing and rendering large JSON data in a pretty-printed format may take additional time and impact browser performance in some cases.
trying to follow instructions like this... https://docs.python.org/2/library/json.html
I can get the terminal to print pretty json, but I want my view to print pretty json in an http response. How do I do this?
def view():
x = {'one': foo, 'two': bar}
return HttpResponse(json.dumps(x, sort_keys=True, indent=4)that is not working for me. The httpresponse is unformatted json