brownzilla
brownzilla

Reputation: 289

Stop python writing json file to one line

How can I force Python to stop writing my .json file to one line? I've tried changing the write command with open('manifest.json', 'wb') as f: to with open('manifest.json', 'w') as f: but it is still writing to one line.

Code

import json

with open('manifest.json', 'r') as f:
    json_data = json.load(f)
    json_data['server-version'] = "xxxx-x.x.x"

with open('manifest.json', 'w') as f:
    f.write(json.dumps(json_data))

print('Successful.')

Upvotes: 20

Views: 28280

Answers (1)

Maksym Kozlenko
Maksym Kozlenko

Reputation: 10363

For pretty printing use indent parameter

print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': ')))

Upvotes: 31

Related Questions