Reformat your json files~

Discussion in 'Mod Discussions' started by shadowmint, June 10, 2013.

  1. shadowmint

    shadowmint New Member

    Messages:
    17
    Likes Received:
    7
    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~
    Last edited: June 14, 2013
  2. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    THANK YOU!
    stuart98 likes this.
  3. garat

    garat Cat Herder Uber Alumni

    Messages:
    3,344
    Likes Received:
    5,376
    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. :)
    RainbowDashPwny likes this.
  4. bgolus

    bgolus Uber Alumni

    Messages:
    1,481
    Likes Received:
    2,299
    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.
    exterminans likes this.
  5. garat

    garat Cat Herder Uber Alumni

    Messages:
    3,344
    Likes Received:
    5,376
    Huh, I had never noticed that, Ben. Maybe I won't use it any more when testing balance changes. :)
  6. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    JSONLint works well http://jsonlint.com/.
    maxpowerz likes this.
  7. garat

    garat Cat Herder Uber Alumni

    Messages:
    3,344
    Likes Received:
    5,376
    Oh, thank you.. Was pretty sure I had been using something else before, but couldn't remember the name. That'd be it.
  8. mzhg

    mzhg Member

    Messages:
    33
    Likes Received:
    0
    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)
  9. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    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.
  10. shadowmint

    shadowmint New Member

    Messages:
    17
    Likes Received:
    7
    oh, that's cool. I was just frowning about the diffs looking weird.
    edit'd in the original post. :)
  11. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    That's exactly what made me look if there was an option for it :lol:
  12. YourLocalMadSci

    YourLocalMadSci Well-Known Member

    Messages:
    766
    Likes Received:
    762
    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.
  13. Gorbles

    Gorbles Post Master General

    Messages:
    1,832
    Likes Received:
    1,421
    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 :p
  14. nossalamis

    nossalamis New Member

    Messages:
    5
    Likes Received:
    0
    Quick question, Is this for python 3.3 or 2.7?
  15. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    I installed the latest version (3.3). It'd probably work on either though.
  16. stuart98

    stuart98 Post Master General

    Messages:
    6,009
    Likes Received:
    3,888
    Is there a way to make this reformat .pfx files, which, so far as I can tell, are jsons with a different name?
  17. Corang

    Corang Well-Known Member

    Messages:
    772
    Likes Received:
    313
    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
  18. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    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)
  19. stuart98

    stuart98 Post Master General

    Messages:
    6,009
    Likes Received:
    3,888
    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.
  20. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    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()
    LordFraggington likes this.

Share This Page