Reputation: 9431
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?
Upvotes: 45
Views: 65916
Reputation: 12795
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
}
Upvotes: 91
Reputation: 41247
The indent
parameter allows some degree of pretty printing.
From the documentation:
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, or negative, will only insert newlines. None (the default) selects the most compact representation.
Upvotes: 6