Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON dumps format Python

Tags:

python

json

I am reading a JSON file, adding a field and then writing to a new JSON file.

The JSON file I read in links.json looks like this:

[{"negativeNode":"osgb4000000023183407","toid":"osgb4000000023296573","term":"Private Road - Restricted Access","polyline":[492019.481,156567.076,492028,156567,492041.667,156570.536,492063.65,156578.067,492126.5,156602],"positiveNode":"osgb4000000023183409","index":1,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023763485","toid":"osgb4000000023296574","term":"Private Road - Restricted Access","polyline":[492144.493,156762.059,492149.35,156750,492195.75,156630],"positiveNode":"osgb4000000023183408","index":2,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023183650","toid":"osgb4000000023296638","term":"Private Road - Restricted Access","polyline":[492835.25,156873.5,493000,156923,493018.061,156927.938],"positiveNode":"osgb4000000023183652","index":3,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023181163","toid":"osgb4000000023388466","term":"Local Street","polyline":[498136.506,149148.313,498123.784,149143.969,498119.223,149143.411,498116.43,149143.318,498113.638,149145.179],"positiveNode":"osgb4000000023806248","index":4,"nature":"Single Carriageway"}
]

I open the JSON file, read it, create a new field and then dump it in a new file:

import json
links_file = open('links.json')
links = json.load(links_file)

for link in links:
    link['length'] = 10

with open('links_new.json','w') as outfile:
    json.dump(links, outfile)

This successfully exports and I am able to inspect with a text editor (Sublime Text):

[{"index": 1, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023183407", "toid": "osgb4000000023296573", "length": 10, "polyline": [492019.481, 156567.076, 492028, 156567, 492041.667, 156570.536, 492063.65, 156578.067, 492126.5, 156602], "positiveNode": "osgb4000000023183409"}, {"index": 2, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023763485", "toid": "osgb4000000023296574", "length": 10, "polyline": [492144.493, 156762.059, 492149.35, 156750, 492195.75, 156630], "positiveNode": "osgb4000000023183408"}, {"index": 3, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023183650", "toid": "osgb4000000023296638", "length": 10, "polyline": [492835.25, 156873.5, 493000, 156923, 493018.061, 156927.938], "positiveNode": "osgb4000000023183652"}, {"index": 4, "term": "Local Street", "nature": "Single Carriageway", "negativeNode": "osgb4000000023181163", "toid": "osgb4000000023388466", "length": 10, "polyline": [498136.506, 149148.313, 498123.784, 149143.969, 498119.223, 149143.411, 498116.43, 149143.318, 498113.638, 149145.179], "positiveNode": "osgb4000000023806248"}]

As you can see, this isn't as visually readable as the original JSON file. I am able to read it line by line successfully but it is printed as one single line in Sublime Text. Is there a formatting side to JSON dumps that I am missing?

like image 904
LearningSlowly Avatar asked May 23 '16 18:05

LearningSlowly


People also ask

What JSON dumps do in Python?

dumps() json. dumps() function converts a Python object into a json string. skipkeys: If skipkeys is True (default: False), then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.

How do I beautify JSON output in Python?

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.

What is JSON dumps () method?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.

What's the difference between JSON dump and JSON dumps?

json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.


1 Answers

There's a parameter called indent. It is None by default, which means "no pretty printing". If you set it to an integer value, it will enable pretty-printing, and use that many spaces to indent nested elements.

In your case it will be something along the lines of:

json.dumps(links, outfile, indent=4)

(or indent=2 if you prefer less spaces). Here's an example from the docs (link), which shows more functionality you might also want as you progress:

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,
...                  indent=4, separators=(',', ': '))
{
    "4": 5,
    "6": 7
}
like image 141
Ishamael Avatar answered Oct 07 '22 19:10

Ishamael