Display curl output in readable JSON format in Unix shell script
A few solutions to choose from:
json_pp: command utility available in Linux systems for JSON decoding/encoding
echo '{"type":"Bar","id":"1","title":"Foo"}' | json_pp -json_opt pretty,canonical{ "id" : "1", "title" : "Foo", "type" : "Bar"}
You may want to keep the -json_opt pretty,canonical
argument for predictable ordering.
jq: lightweight and flexible command-line JSON processor. It is written in portable C, and it has zero runtime dependencies.
echo '{"type":"Bar","id":"1","title":"Foo"}' | jq '.'{ "type": "Bar", "id": "1", "title": "Foo"}
The simplest jq
program is the expression .
, which takes the input and produces it unchanged as output.
For additinal jq
options check the manual
with python:
echo '{"type":"Bar","id":"1","title":"Foo"}' | python -m json.tool{ "id": "1", "title": "Foo", "type": "Bar"}
echo '{"type":"Bar","id":"1","title":"Foo"}' | node -e "console.log( JSON.stringify( JSON.parse(require('fs').readFileSync(0) ), 0, 1 ))"{ "type": "Bar", "id": "1", "title": "Foo"}
This is to add to of Gilles' Answer. There are many ways to get this done but personally I prefer something lightweight, easy to remember and universally available (e.g. come with standard LTS installations of your preferred Linux flavor or easy to install) on common *nix systems.
Here are the options in their preferred order:
Python Json.tool module
echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
pros: almost available everywhere; cons: no color coding
jq (may require one time installation)
echo '{"foo": "lorem", "bar": "ipsum"}' | jq
cons: needs to install jq; pros: color coding and versatile
json_pp (available in Ubuntu 16.04 LTS)
echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp
For Ruby users
gem install jsonprettyecho '{"foo": "lorem", "bar": "ipsum"}' | jsonpretty