Will Roe's blog

Anaλysis Paraλysis » Archives

JSON is a fairly easy to read format but sometimes you need to deal with an unreadable dump of the stuff and it’d be useful to quickly reformat or prettify it. There are a number of ways to do this.

Javascript (Chrome, Firefox etc.)

Javascript has the JSON.stringify function, which can pretty-print JSON according to the number of spaces you pass as the 3rd parameter. For example:

var example = {user: {name: "Ada Lovelace", email: "ada@example.com"}, group: "programmers"}
JSON.stringify(example, undefined, 4);

If you try the above in a console in a web browser (or a Node.js console), you should see this:

{
    "user": {
        "name": "Ada Lovelace",
        "email": "ada@example.com"
    },
    "group": "programmers"
}

json_reformat

That’s all well and good, but relying on a javascript console is not the most efficient way to do things, so an easier and more scritable tool is the json_reformat command (part of the Yajl). To get this tool installed, do the following:

  • brew install yajl - OS X
  • apt-get install yajl-tools - Debian/Ubuntu Linux

Now you can just pipe JSON text to json_reformat and it will pretty-print it to standard output. This makes it very useful for use in other tools, such as vim. Here is some vim config for using json_reformat:

au BufRead,BufNewFile *.json set filetype=json
au FileType json setlocal equalprg=json_reformat

Now let’s edit a file that was generated by Berkshelf and has been spat out on the filesystem in ~/.berkshelf/config.json. Initially it looks like this:

{"chef":{"chef_server_url":"https://api.opscode.com/organizations/acmecorp","validation_client_name":"acmecorp-validator","validation_key_path":"/Users/ada/Work/.chef/acmecorp-validator.pem","client_key":"/Users/ada/Work/.chef/ada.pem","node_name":"ada"},"cookbook":{"copyright":"Ada Lovelace","email":"ada@example.com","license":"MIT"},"allowed_licenses":[],"raise_license_exception":false,"vagrant":{"vm":{"box":"Berkshelf-CentOS-6.3-x86_64-minimal","box_url":"https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box","forward_port":{},"network":{"bridged":false,"hostonly":"33.33.33.10"},"provision":"chef_solo"}},"ssl":{"verify":false}}

This passive-aggressive attempt by the machines to subjugate us humans just will not stand and thankfully we have the tools to take back control of our JSON configs. Position the cursor anywhere on the single line of JSON and press ‘==’, vim will pipe the buffer to json_reformat and replace it with the output of that command resulting in this:

{
    "chef": {
        "chef_server_url": "https://api.opscode.com/organizations/acmecorp",
        "validation_client_name": "acmecorp-validator",
        "validation_key_path": "/Users/ada/Work/.chef/acmecorp-validator.pem",
        "client_key": "/Users/ada/Work/.chef/ada.pem",
        "node_name": "ada"
    },
    "cookbook": {
        "copyright": "Ada Lovelace",
        "email": "ada@example.com",
        "license": "MIT"
    },
    "allowed_licenses": [

    ],
    "raise_license_exception": false,
    "vagrant": {
        "vm": {
            "box": "Berkshelf-CentOS-6.3-x86_64-minimal",
            "box_url": "https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box",
            "forward_port": {

            },
            "network": {
                "bridged": false,
                "hostonly": "33.33.33.10"
            },
            "provision": "chef_solo"
        }
    },
    "ssl": {
        "verify": false
    }
}

This trick with vim and json_reformat is one of the many answers to a StackOverflow question about pretty-printing JSON, there are so many more.