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 OverflowUse 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.
How do you print json data in multiply lines?
JSON and Dictionary
Manipulating JSON dictionaries
I think you are saying that you have JSON-encoded data, and you want to pretty print it.
In that case, use this:
import json
with open("myfile.json") as I:
print(json.dumps(json.load(I), indent=1))All that does is load the JSON data into a Python dictionary, and then re-dumps it as a string with pretty indentation.
I'm not sure that you're clear on your terminology, though, which may cause a lot of confusion for you down the line. A dictionary is the same as an object in Javascript: a mapping of keys to values. JSON stands for Javascript Object Notation, meaning that it's an encoding format for Javascript Objects which conveniently matches the literal syntax for Javascript Objects, and some types of Python Dictionary. But, JSON is just the encoding format, the underlying data is termed an Object or Dictionary, or whatever, depending on the language.
More on reddit.comVideos
Currently trying to get some data from a url which shows in json format, i can get the data, however when i print it, it just shows in 1 long line of text which isnt what i want. i want the text to be split into multiply lines like when you add \n to strings. (i know its normally not good to do except Exceptions, its just there while i get the other part to work, also the entire def is in a class)
Here is what i currently have. I havent work much with json data before which is why im stuck at what exactly to do.
def info(self):
try:
url = [url]
response = requests.get(url)
x = json.loads(response.text)
lore = str(x['data'][input_champion]['lore'])
print('Getting champion info, please wait')
time.sleep(5)
print(f'lore: {lore}')
time.sleep(0.5)
except Exception as e:
time.sleep(5)