Back to TIL list

Formatar JSON no Vim

Created at

Antes: Um exemplo de json colapsado

{ "quiz": { "sport": { "q1": { "question": "Which one is correct team name in NBA?","options": [ "New York Bulls", "Los Angeles Kings", "Golden State Warriros","Huston Rocket" ], "answer": "Huston Rocket" } }, "maths": { "q1":{ "question": "5 + 7 = ?", "options": [ "10", "11", "12", "13" ], "answer":"12" }, "q2": { "question": "12 - 8 = ?", "options": [ "1", "2", "3", "4" ],"answer": "4" } } } }

Depois: endentado

{    "quiz": {        "maths": {            "q1": {                "answer": "12",                "options": [                    "10",                    "11",                    "12",                    "13"                ],                "question": "5 + 7 = ?"            },            "q2": {                "answer": "4",                "options": [                    "1",                    "2",                    "3",                    "4"                ],                "question": "12 - 8 = ?"            }        },        "sport": {            "q1": {                "answer": "Huston Rocket",                "options": [                    "New York Bulls",                    "Los Angeles Kings",                    "Golden State Warriros",                    "Huston Rocket"                ],                "question": "Which one is correct team name in NBA?"            }        }    }}

Crie um atalho para executar um comando externo.

Usando python

nmap =j :%!python -m json.tool<CR>

Usando jq

nmap =j :%!jq .<CR>