Skip to content

Instantly share code, notes, and snippets.

@RichardHan
Last active March 16, 2016 02:59
Show Gist options
  • Save RichardHan/ba817019b76564cc8787 to your computer and use it in GitHub Desktop.
Save RichardHan/ba817019b76564cc8787 to your computer and use it in GitHub Desktop.
Unix shell pretty-print JSON in unix shell
#
# Use JSON.stringify
#
$ node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));" \
'{"key1":"value1","key2":"value2"}'
$ node -e "console.log(JSON.stringify(JSON.parse(require('fs') \
.readFileSync(process.argv[1])), null, 4));" myjson.json
# =============================================================================================
#
# With Python 2.6+
# [for convenience you can make an alias] alias prettyjson='python -m json.tool'
#
$ echo '{"key1": "value1", "key2": "value2"}' | python -m json.tool
$ python -m json.tool my_json.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment