Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

[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
            }
        }
        ]
like image 321
nonopolarity Avatar asked Jan 12 '13 02:01

nonopolarity


People also ask

How do I indent in JSON?

JSON conventions¶ Format JSON files to be human readable. Use four spaces for indentation (matching OpenStack conventions used in Python and shell scripts). Do not use tab characters in the code, always use spaces. Use one space after the name-separator (colon).


2 Answers

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

Put the following script in
~/Library/Containers/BBEdit/Data/Library/Application Support/BBEdit/Text Filters/FormatJSON.sh (on MacOS 11 Big Sur, or above)

For MacOS 10.15 Catalina and below, use this location: ~/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 "Spinning Beachball of Death" busy-wait mouse cursor.

like image 186
Mark Avatar answered Oct 07 '22 20:10

Mark


I just corrected this issue in the bundle, for 2.0 users the bundle should update within 24 hours with the correction.

like image 42
Michael Sheets Avatar answered Oct 07 '22 21:10

Michael Sheets