Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 489
xenteros Avatar asked Jan 25 '23 13:01

xenteros


1 Answers

This could also be done in pure Vim:

%delete | 0put =json_encode(json_decode(@@))

But note that the field order within an object will not be preserved. So you can get

{"email":"[email protected]","name":"John"}
like image 188
Matt Avatar answered Jan 28 '23 04:01

Matt