Skip to content

Instantly share code, notes, and snippets.

@fundon
Created April 9, 2012 15:55
Show Gist options
  • Save fundon/2344394 to your computer and use it in GitHub Desktop.
Save fundon/2344394 to your computer and use it in GitHub Desktop.
use JSON.stringify to pretty-print JSON.
function formatjson() {
local json=""
if [ -p /dev/stdin ]; then
# piping, e.g. `echo '{"foo":42}' | formatjson`
while read -r line; do
json="$json$line"
done
else
# e.g. `formatjson '{"foo":42}'`
json="$*"
fi
node -e "console.log(JSON.stringify(eval($json), null, 2));"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment