Format json with Vim (with python)

I want to bring an unformatted json and format it with vim for easy viewing.

  1. For example, the value obtained by hitting some API
  2. For example, the value listed in some document
  3. For example, a hand-crafted value to leave a sample in the document

etc. In the first example, you can display it neatly by using the browser extension, but in the second and third cases, it seems convenient if you can paste it into vim and format it.

In the environment where python2.6 or later is installed (with the python command in the PATH), you can format the json string in the buffer by hitting the following command.

:%!python -m json.tool

For example, if you hit the above command when such a string is in the buffer

{"count":1,"list":[{"url":"http://json.org/example.html","title":"\u3042\u3044\u3046\u3048\u304a"}]}

I feel like this.

{
    "count": 1, 
    "list": [
        {
            "title": "\u3042\u3044\u3046\u3048\u304a", 
            "url": "http://json.org/example.html"
        }
    ]
}

It feels good. However, I'm not very happy with the Japanese being escaped (I'm also worried about the half-width space at the end of the line). So, I made a slightly improved command. I'm new to vim, so I'd be happy if you could get me a tsukkomi.

command! JsonFormat :execute '%!python -m json.tool'
  \ | :execute '%!python -c "import re,sys;chr=__builtins__.__dict__.get(\"unichr\", chr);sys.stdout.write(re.sub(r\"\\u[0-9a-f]{4}\", lambda x: chr(int(\"0x\" + x.group(0)[2:], 16)), sys.stdin.read()))"'
  \ | :%s/ \+$//ge
  \ | :set ft=javascript
  \ | :1

If you write a brief explanation

  1. Formatted with python json.tool
  2. Decode unicode strings like "\ u307d"
  3. Delete because there is a space at the end of each line
  4. Highlight vim filetype as javascript
  5. Move the cursor to the first line

When you execute the command

:JsonFormat

The display will be as follows.

{
    "count": 1,
    "list": [
        {
            "title": "AIUEO",
            "url": "http://json.org/example.html"
        }
    ]
}

Recommended Posts

Format json with Vim (with python)
[Python] Use JSON with Python
Easily format JSON in Python
POST json with Python3 script
String format with Python% operator
Read json data with python
Output log in JSON format with Python standard logging
Read json file with Python, format it, and output json
Python #JSON
Automatically format Python code in Vim
JSON encoding and decoding with python
Build Vim with MinGW. (+ lua, + python)
Merge JSON format data with Ansible
[Python] Format when to_csv with pandas
FizzBuzz with Python3
Python string format
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Convert Excel data to JSON with python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
Reading and writing JSON files with Python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Python string format
format in python
Excel with Python
Microcomputer with Python
Cast with python
Read JSON with Python and output as CSV
Automatically format Python code into PEP8-compliant code with Emacs
[Python3] Read and write with datetime isoformat with json
Install vim7.3 (+ python2.4) from source (compatible with Gundo.vim)
Make JSON into CSV with Python from Splunk
Homebrew Vim doesn't start with Python 3.8 error Note
Convert strings to character-by-character list format with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
python variable expansion, format
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER