How to indent JSON data inside of TextMate, Emacs, BBEdit, or Sublime Text 2?

nonopolarity picture nonopolarity · Jan 12, 2013 · Viewed 11.5k times · Source

[Update: 8 hours after this question was posted, the author of JSON bundle was notified of the issue and he fixed it.]

I have the following JSON data in a file application.json, shown at the end of this post, and I have used TextMate with the JSON bundle, Emacs, BBEdit, and Sublime Text 2 to properly indent it, but all seemed like they couldn't.

Both TextMate and Sublime Text 2 insisted that the first { should not be indented, and the first major issue was for the closing brace for "child": {. Both TextMate and Sublime Text 2 refused to align the } under the left side of "child": {. Emacs kept on indenting further and further for each line, and BBEdit didn't seem to have an re-indent function at all (could this be?).

Is there a way to properly indent the file, or are TextMate and Sublime Text 2 both doing the right thing for the JSON data?

[ 
{
    "settings": [ "master" ],
    "appPort": "8666",
    "specs": {
        "frame" : {
            "type" : "HTMLFrameMojit",

            "config": {
                "deploy": true,
                "child": {
                    "type" : "HelloWorldMojit"
                    },
                    "assets": {
                        "top": {
                            "css": [
                            "/static/HelloWorldMojit/assets/index.css"
                            ]
                        }
                    }
                }
            }
        }
        },
        {
            "settings": [ "environment:development" ],
            "staticHandling": {
                "forceUpdate": true
            }
        }
        ]

Answer

Mark picture Mark · Oct 17, 2013

I found a solution for BBEdit that is easy and works well.

Put the following script in
~/Library/Application Support/BBEdit/Text Filters/FormatJSON.sh

#!/bin/bash
python -m json.tool
  1. Open a JSON file in BBEdit. There is no need to restart BBEdit because BBEdit rocks!
  2. Select Text > Apply Text Filter > FormatJSON

I tested this with a JSON file that had 3,612,683 characters on a single line. BBEdit opened this file and reformatted without showing a SPOD.