Display curl output in human readable JSON format in a Unix shell script

In my Unix shell script, when I execute the curl command, the result will be displayed as below, which I redirect to the file:

{"type":"Show","id":"123","title":"name","description":"Funny","channelTitle":"ifood.tv","lastUpdateTimestamp":"2014-04-20T20:34:59","numOfVideos":"15"} 

But I want this output to be placed in a readable JSON format, as shown in the file below:

 {"type":"Show", "id":"123", "title":"name", "description":"Funny", "channelTitle":"ifood.tv", "lastUpdateTimestamp":"2014-04-20T20:34:59", "numOfVideos":"15"} 

How do I format the output in this way?

+168
json shell curl
Dec 01 '14 at 10:18
source share
9 answers

Try to do this:

 curl ... | json_pp 

or using jq using an identification filter:

 curl ... | jq '.' 

enter image description here

or with and bash :

 curl ... | node <<< "var o = $(cat); console.log(JSON.stringify(o, null, 4));" 

Check out https://stedolan.imtqy.com/jq/

+411
Dec 01 '14 at 22:23
source share
— -

I assume you want to outperform JSON output. This can be achieved with python:

curl http://localhost:8880/test.json | python -mjson.tool > out.json

+36
Dec 01 '14 at 22:24
source share
  1. brew install jq
  2. command + | jq
  3. (example: curl localhost:5000/blocks | jq. )
  4. Enjoy it!

enter image description here

+32
Sep 15 '17 at 20:39 on
source share

I found json_reformat very convenient. So I just did the following:

 curl http://127.0.0.1:5000/people/api.json | json_reformat 

what is it!

+5
Jul 20 '16 at 10:17
source share
 python -m json.tool Curl http://127.0.0.1:5000/people/api.json | python -m json.tool 

may also help

+5
Mar 16 '19 at 4:25
source share

This needs to be added to Gill's answer. There are many ways to do this, but I personally prefer something lightweight, easy to remember, and universally accessible (for example, supply a standard LTS installation with your preferred Linux style or easily installable) on shared * nix systems.

Here are the options in their preferred order:

  • Python module Json.tool, for example, echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool (pros: almost everywhere, cons: no color coding)

  • jq (may require a one-time installation) echo '{"foo": "lorem", "bar": "ipsum"}' | JQ (cons: need to install jq; pros: color coding and universal)

  • json_pp (available on Ubuntu 16.04 LTS), for example. echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp

  • For Ruby users, gem install jsonpretty echo '{"foo": "lorem", "bar": "ipsum"}' | jsonpretty

+4
May 10 '17 at 1:27
source share

Check curljson

 $ pip install curljson $ curljson -i <the-json-api-url> 
+4
Aug 29 '18 at 10:02
source share

You can use this node module

[sudo] npm i -g json ; // suggest not using root privileges to install the node module

then just add |json after curl. curl http://localhost:8880/test.json |json

+3
Jul 02 '19 at 9:35
source share

Just add the ?pretty keyword to your curl as- command
If your curl command looks like this: curl -XGET 'http://localhost:9200/_cluster/state'
having pretty weekend use like this is
curl -XGET 'http://localhost:9200/_cluster/state'?pretty

-one
Jan 31 '17 at 12:39 on
source share



All Articles