Publicado em: 19/Nov/2018
Atualizado em: 03/Mar/2020
#vim

Formatar JSON no Vim

Antes: Um exemplo de json colapsado

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

Depois: endentado

 1{
 2    "quiz": {
 3        "maths": {
 4            "q1": {
 5                "answer": "12",
 6                "options": [
 7                    "10",
 8                    "11",
 9                    "12",
10                    "13"
11                ],
12                "question": "5 + 7 = ?"
13            },
14            "q2": {
15                "answer": "4",
16                "options": [
17                    "1",
18                    "2",
19                    "3",
20                    "4"
21                ],
22                "question": "12 - 8 = ?"
23            }
24        },
25        "sport": {
26            "q1": {
27                "answer": "Huston Rocket",
28                "options": [
29                    "New York Bulls",
30                    "Los Angeles Kings",
31                    "Golden State Warriros",
32                    "Huston Rocket"
33                ],
34                "question": "Which one is correct team name in NBA?"
35            }
36        }
37    }
38}

Crie um atalho para executar um comando externo.

Usando python

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

Usando jq

1nmap =j :%!jq .<CR>
comments powered by Disqus