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:
Latest source codes could be found in GitHub.
Install it globally by using Composer:
Suppose there is JSON data in data.json as below:
data.json and output to screen:
data.json and save to another file
The JSON data after formatted would be like:
It’s possible to reverse the behavior, say, remove all the white-spaces by passing
0 as the 2nd parameter like:
More options depend on the JSON constants provided.
Format current file:
Or add a keymap in