William Roe’s blog

A variety of geekery

JSON for Humans

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:

1
2
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:

1
2
3
4
5
6
7
{
    "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:

1
2
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:

1
{"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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
    "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.

Tmux & Autoenv Tip

On OS X, if I use tmux (or more usually, wemux for pairing) then I end up with oddly named windows like this:

This is because I’ve followed the awesome instructions from Dr Bunsen’s Text Triumvirate and in order to have the tmux and system (OS X) clipboards interacting, it requires a hack called reattach-to-user-namespace which proxies the running of zsh in tmux.

One quick way to get around this and save time renaming my tmux windows, was to use autoenv and add a file named .env to projects with content similar to:

tmux rename-window "project-name"

This is piling hacks on top of hacks, but at least it saves a bit of time for frequently visited directories.

A Silver Searcher Gotcha

There is a gotcha with the silver searcher and how it treats .gitignore files. The following pattern will work as expected with git:

/log

I.e. git will ignore everything in that directory, however The Silver Searcher doesn’t understand that and it’ll still show matching lines from files in log/. For git and ag (The Silver Searcher) to both ignore that directory and its contents, change /log to log.

Tweet