Daniel Keast

Edit minified json file in vim

linux

If you need to edit a json file in vim that has been minified like this:

{"test":1,"small":"json","hard":["to","edit"]}

Type the command:

:%!jq .

The ‘%’ character causes the buffer to be placed into the stdin of the external command to be run, and when it’s finished replace the buffer with it’s stdout.

The jq command pretty-prints it’s output by default, so giving it the path ‘.’ causes it format the entire document. Once jq has finished, your buffer will look like this:

{
  "test": 1,
  "small": "json",
  "hard": [
    "to",
    "edit"
  ]
}

Once you’re finished, run this command to re-minify the json:

:%!jq -c .