JSON Pretty

2023-12-25 json formatting pretty-print jq python perl JSON::XS

Working with JSON files in Visual Studio Code is quite convenient with nice features. But when the files are large, especially with everything on single line, it all stops working. A solution would be to reformat the file, so I looked for some options.

My file is around 14MB, exported from a web service.

With brief googling, I found solution using python’s json.tool:

type file.json | python -m json.tool >file-pretty.json

Then it occurred to me that something similar will be available in perl, since the JSON::XS module can do the pretty-printing, too. And there is a command-line tool json_xs:

type file.json | json_xs >file-pretty.json

And when searching this very site, I found another option in jq tool presented in this post. Its simplest usage with automatically pretty-print the output:

jq . file.json > file-pretty.json

Each option generate slight different formatting and encoding, especially for the unicode characters. All output (they are around 20MB) can be easily opened with the Visual Studio Code and worked with easily.