Home Of MetaWops.

OS X. iOS. Gadgets. Social Media. Basketball. Music. Synths. Electronics.

Software, Tipps & Tricks

JSON pretty print for TextMate 2.0

TextMate2iconFor quite some time now the great OS X text editor TextMate has gone open source in version 2.0 (version 1.0 can still be bought, though). It’s still in alpha but works quite well for me plus there’s a very active development community so that updates arrive on a nearly daily basis. (Yeah, I know about Sublime Text 2 and the ‚fight‘ between the two about which one’s the best. I like both. :-))

However, the other day I was using TextMate 2.0 and working on some Twitter API stuff and had to handle a rather large JSON response. Problem was: the returned JSON wasn’t nicely formatted and I wanted to study it. There is a JSON bundle in TextMate 2.0 and it contains a single command called „Reformat Selection“. I tried that on my long JSON string but the command never came back and did nothing and I had to kill TextMate. 🙁

A bit of googling brought this nifty little gem to light and I dove a bit into TextMate 2.0 bundle writing. Turns out: bringing that python quickie into TextMate is a piece of cake. Just open the bundle editor (Bundles>Edit Bundles…), select „JSON“ in the leftmost column, then „Menu Actions“ in the 2nd column. Press Cmd-N, select „Command“ and press the Create button. Then fill out the form and the script area according to this screenshot:

TextMate 2.0 JSON pretty printOf course, you can choose another keyboard shortcut or name of that command. The source code lines #1 and #2 are automatically generated so you only have to enter the 3rd line. Press Cmd-S to save that bundle and try your new command on you favorite JSON string. Worked like a charm for me. 🙂

  1. nAv

    It looks like reformat document had a bug. Under „Output“ you have to select Replaced Selected Text

  2. We have since fixed the built-in prettifier to not hang on JSON larger than the io buffer (it was a deadlock caused by doing read/write to pipe from same thread).

  3. Awesome news, Allan, thanks for letting me know! 🙂

  4. 1kg2

    Great. Could you post the code to be able to copy & paste, in addition to the screen shot? Thanks!

    • Drop me another line if you (a) still think you need it after Allan stated above that the bug doesn’t exist anymore and (b) are really not able to type the three lines from the screenshot. C’mon! Really?!

  5. Charles Bourasseau

    I think this ist now standard in TextMate2.
    I already had the action „Reformat Document / Selection“.

  6. How can you accomplish the same reformatting, without reordering the entire JSON alphabetically (thing i hate especially after being familiar with certain output for long time, and now having to scroll down to find items alphabetically ordered, instead of ordered as you defined them in your DTO – bundled related fields together)

  7. Jason

    Thanks! You can also use some online tools. I find JSON pretty print to be the easiest.

Schreibe eine Antwort

Theme von Anders Norén

%d Bloggern gefällt das: