It’s easy to write a CLI tool of JSON Formatter by PHP, since a new JSON constant JSON_PRETTY_PRINT is available as of PHP 5.4.0. Essential implementation looks like:


echo json_encode(json_decode(file_get_contents($argv[1])), 
                 $argv[2] ?? JSON_PRETTY_PRINT);

Latest source codes could be found in GitHub.

Install it globally by using Composer:

$ composer global require codegear/json-formatter

Suppose there is JSON data in data.json as below:

{"id":"7108-1062","no_file":[{"Identifier":"61316","Name":"DHS800 Dissertation Tasks (Model)","Code":"DHS800-MOD"},{"Identifier":"126922","Name":"MGT301 Principles of Management (Model)","Code":"MGT301-MOD"}],"no_file_count":23}

Format file data.json and output to screen:

$ jf data.json

Format file data.json and save to another file data_formatted.json:

$ jf data.json > data_formatted.json

The JSON data after formatted would be like:

    "id": "7108-1062",
    "no_file": [
            "Identifier": "61316",
            "Name": "DHS800 Dissertation Tasks (Model)",
            "Code": "DHS800-MOD"
            "Identifier": "126922",
            "Name": "MGT301 Principles of Management (Model)",
            "Code": "MGT301-MOD"
    "no_file_count": 23

It’s possible to reverse the behavior, say, remove all the white-spaces by passing 0 as the 2nd parameter like:

$ jf data_formatted.json 0


$ jf data_formatted.json 0 > data.json

More options depend on the JSON constants provided.


Format current file:

:%!jf %

Or add a keymap in vimrc:

nnoremap <Leader>jf :%!jf %<CR>