Pretty printing JSON and XML documents

26 Oct 2017 in JSON, Shell, XML

I work many times in command line and here's how I'm pretty printing JSON documents:

# get file contents via cat, curl, wget, etc
cat <path/to/file.json> | python -mjson.tool

You may also use the jq command line utility (using the "." filter in order to pipe the result):

cat <path/to/file.json> | jq "." | less 

For XML documents I use xmllint:

cat <path/to/file.xml> | xmllint --format -

If you have a schema file you can validate the document using something like:

xmllint --noout --schema <path/to/schema.xsd> <path/to/file.xml>