Might be useful to some here; rewrites the json files in all subdirectories it's run from to be pretty; makes viewing patch diffs easier. Code: import os import json import string from os.path import join for root, dirnames, filenames in os.walk('.'): for f in filenames: f = f.lower() if f[-5:] == ".json": fullpath = join(root, f) print("%s" % fullpath) fp = open(fullpath) data = json.load(fp) fp.close() fp = open(fullpath, 'w') fp.write(json.dumps(data, indent=2, sort_keys=True)) fp.close() (It's python, obviously; save to reformat.py and run from the media/pa folder) edit: thanks raevn! sort_keys cool~
Another good site: http://jsonformat.com/ Our JSON, of course, when we're working with it, is heavily formatted, but our build tools cut out all the new lines, and extraneous junk that confuse parsers.
That's handy. We strip the white space when building for a (likely very minor) perf improvement. I was thinking of asking to not strip them so they remain readable, but using that script they're probably even more cleanly formatted than our internal ones. edit: garat, that site mangles the json formatting pretty horribly, not to mention has some number precision issues.
Oh, thank you.. Was pretty sure I had been using something else before, but couldn't remember the name. That'd be it.
As said in another post, if you are using Firefox, there is a addons which allow you to open json file into FF, nicely formatted, highlighted and with collapse feature. You can then bind .json file to open with FF directly http://jsonview.com There is also a unofficial Chrome version. (bottom right of the page)
Changing Code: fp.write(json.dumps(data, indent=2)) to Code: fp.write(json.dumps(data, indent=2, sort_keys=True)) makes comparing versions much simpler.
Incidentally, if you don't know where to start with code writing, I recommend a good, free, fairly lightweight, text and code editor for windows as Notepad++. It's not a full IDE, but it has more features than basic notepad if you are on windows. More experienced people may be able to recommend better software, but it's the one I use. More relevantly, it has a JSON file formatting plugin. It's easily installed by the bundled plugin manager. So far, it works on all the files I have tested it on, however it used tab for line indenting, rather than space, which may or may not be an issue.
I use Sublime Text 2, personally. Though with regards to text files I have an annoying habit of writing my own specific editors. Not for JSON though, no bloody point
Is there a way to make this reformat .pfx files, which, so far as I can tell, are jsons with a different name?
As I did this awhile ago i cant really remember if it worked or not, but I know I tried to change the file extension in the code from .json to .pfx, I cant remember if it worked but it is worth a shot
Change this line: Code: if f[-5:] == ".json": to Code: if f[-5:] == ".json" or f[-4:] == ".pfx": Note: the -5 becomes -4 for pfx, since it's only 3 characters, not 4. (untested)
Ended up copying the pfxs to a new folder, batch renaming them via command prompt (ren *.old *.new), running the normal reformat.py, and batch renaming them back. Worked fine.
You'd need to do that for every new build though... If the previous change doesn't work, a foolproof, tested (but ugly) way is: Code: import os import json import string from os.path import join for root, dirnames, filenames in os.walk('.'): for f in filenames: f = f.lower() if f[-5:] == ".json": fullpath = join(root, f) print("%s" % fullpath) fp = open(fullpath) data = json.load(fp) fp.close() fp = open(fullpath, 'w') fp.write(json.dumps(data, indent=2, sort_keys=True)) fp.close() if f[-4:] == ".pfx": fullpath = join(root, f) print("%s" % fullpath) fp = open(fullpath) data = json.load(fp) fp.close() fp = open(fullpath, 'w') fp.write(json.dumps(data, indent=2, sort_keys=True)) fp.close()