vim, git, aws and other three-letter words

Software Development Notes

How to reformat a JSON file

There are several tools that can be used to format a large JSON file.

Prettier (if you have node.js and npx installed):

npx run prettier input_json.json > formatted_json.json

With python:

cat input_json.json | python -m json.tool > formatted_json.json

# json.tool uses 4 spaces as indent by default, we can change it:
cat input_json.json | python -m json.tool --indent 2 > formatted_json.json

With jq:

jq '.' input_json.json > formatted_json.json

Reminder: to open a large file in vim / nvim it is better to run it with -u NONE: nvim -u NONE formatted_json.json.

Tags: shell