bspckl bspckl - 4 months ago 19
JSON Question

VIM: swap json values within double quotes

I need to swap tag and value in a json file on multiple lines using VIM editor.

ex:
this:

{"value":"PAE","tag":"project:aerospace;id:3364"}
{"value":"#cybersecurity","tag":"project:aerospace;id:3178"}
{"value":"Boeing","tag":"project:aerospace;id:3342"}
{"value":"Airbus","tag":"project:aerospace;id:3335"}


needs to be:

{"tag":"project:aerospace;id:3364","value":"PAE"}
{"tag":"project:aerospace;id:3178","value":"#cybersecurity"}
{"tag":"project:aerospace;id:3342","value":"Boeing"}
{"tag":"project:aerospace;id:3335","value":"Airbus"}


I have gotten as far as
:%s/tag/value/g
(using a temp) for the tag and value, but I need to know how to swap either everything within the 2nd and 4th double quotes or everything before and after the comma.

Answer
%norm! f,xdT{f}i,^R"

In above cmd, ^R you press ctrl-v ctrl-r

Comments