Search code examples
pythonjsonbashvimpretty-print

Is there an easy way to reverse pretty print of json in vim?


When I label images or text for Machine Learning purposes, I often export the results in a json format. Then, I can open it in vim and simply pretty print using

:execute '%!python -m json.tool'

I often add | w which automatically writes changes to the file.

Is there a way to reverse this process? To compact the json, so there are no redundant characters?

Example input:

{
    "name": "John",
    "email": "[email protected]"
}

Desired output:

{"name":"John","email":"[email protected]"}

I would be fulfilled with Vimish, Pythonish and Bashish solution.


Solution

  • As chepner mentioned in the comment the solution is to use:

    :%!jq -c .
    

    I have tested it and it works.

    In case, one wants to save the file immediately, they can add | w to write changes.

    This requires jq installed on the system which is quite a standard utility.