Skip to content

Instantly share code, notes, and snippets.

@buchireddy
Created February 13, 2016 01:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save buchireddy/721edc283b46cb9265e3 to your computer and use it in GitHub Desktop.
Save buchireddy/721edc283b46cb9265e3 to your computer and use it in GitHub Desktop.
Beautify a JSON file with python
import simplejson as json
obj = None
with open('file.json') as f:
obj = json.load(f)
outfile = open('file.json', "w")
outfile.write(json.dumps(obj, indent=4, sort_keys=True))
outfile.close()
@mathe999
Copy link

Hello, @buchireddy.

I don't know if I made any major mistakes but everytime I try to dump it it ends up dumping a string containing tabs and /n in one line.

Is there any way to avoid this?

Best regards, Mathe

@buchireddy
Copy link
Author

@mathe999 This is an old script that I've written but I think there is an awesome jq (https://stedolan.github.io/jq/) that I would recommend now. Can you try that/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment