summaryrefslogtreecommitdiff
path: root/content/posts/format-json-with-python.md
blob: b7524a178dc77ff5b4607096230177eede2fc2a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
title: "Format JSON with Python (oneliner)"
description: "Use python to format JSON data"
date: 2024-12-19T16:00:00+02:00
tags: ['python', 'json', 'cli']
---

I'm experimenting with getting by without Language Server Protocol (LSP), and today I came a accross a large JSON file that was compressed into a single line. As I usually do I typed `<leader>+f` to make the LSP format the file.. I soon realized that I had to find another way without my LSP.

<!--more-->

So I came across this python3 module `json.tool` that can format JSON input.

To format a file:

```bash
python3 -m json.tool unformatted.json > formatted.json
```

To format from stdin:

```bash
cat unformatted.json | python3 -m json.tool -- > formatted.json
```

We can also do it from within vim thanks to vim's support of external commands. Let's say we have the following line in our editor.

```json
{ "name": "claw0ry", "website": "https://claw0ry.net"}
```

Let's select the line with `V` and then type

```
:'<,'>!python3 -m json.tool --
```

And VOILA, the data is formatted.

```json
{
    "name": "claw0ry",
    "website": "https://claw0ry.net"
}
```

## Alternative

Another alternative is to use `jq` which is way less characters to type, but it requires you to have the tool installed and the python module is built-in.