Display curl output in readable JSON format in Unix shell script

JsonShellCurl

Json Problem Overview


In my Unix shell script, when I execute a curl command, the result will be displayed as below which I am redirecting to 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 put in the readable JSON format like below in the file:

{"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 this way?

Json Solutions


Solution 1 - Json

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.


> [tag: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 [tag:python]:

echo '{"type":"Bar","id":"1","title":"Foo"}' | python -m json.tool
{
    "id": "1",
    "title": "Foo",
    "type": "Bar"
}

with [tag:nodejs] and [tag:bash]:

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"
}

Solution 2 - Json

I am guessing that you want to prettify the JSON output. That could be achieved using python:

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

Solution 3 - Json

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 jsonpretty
echo '{"foo": "lorem", "bar": "ipsum"}' | jsonpretty

Solution 4 - Json

You can use the json node module:

npm i -g json

then simply append | json after curl. curl http://localhost:8880/test.json | json

Solution 5 - Json

python -m json.tool
Curl http://127.0.0.1:5000/people/api.json | python -m json.tool

can also help.

Solution 6 - Json

I found json_reformat to be very handy. So I just did the following:

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

that's it!

Solution 7 - Json

You can install jq and make the query like below:

curl http://localhost:8080/observations/station/221 | jq

enter image description here

Solution 8 - Json

Motivation: You want to print prettify JSON response after curl command request.

Solution: json_pp - commandline tool that converts between some input and output formats (one of them is JSON). This program was copied from json_xs and modified. The default input format is json and the default output format is json with pretty option.

Synposis: json_pp [-v] [-f from_format] [-t to_format] [-json_opt options_to_json1[,options_to_json2[,...]]]

Formula: <someCommand> | json_pp

Example:

Request

curl -X https://jsonplaceholder.typicode.com/todos/1 | json_pp 

Response

{
   "completed" : false,
   "id" : 1,
   "title" : "delectus aut autem",
   "userId" : 1
}

Solution 9 - Json

Check out curljson

$ pip install curljson
$ curljson -i <the-json-api-url>

Solution 10 - Json

With [tag:xidel]:

curl <...> | xidel - -se '$json'

xidel can probably retrieve the JSON for you as well.

Solution 11 - Json

A lot more features (slice, filter and map and transform structured ) apart from formatting.

https://stedolan.github.io/jq/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamsView Question on Stackoverflow
Solution 1 - JsonGilles QuenotView Answer on Stackoverflow
Solution 2 - Json0xbbView Answer on Stackoverflow
Solution 3 - JsonZhenhuaView Answer on Stackoverflow
Solution 4 - JsonNanoNovaView Answer on Stackoverflow
Solution 5 - JsonVishnuView Answer on Stackoverflow
Solution 6 - JsonRaptorView Answer on Stackoverflow
Solution 7 - JsonArefeView Answer on Stackoverflow
Solution 8 - JsonavivamgView Answer on Stackoverflow
Solution 9 - JsonmitnkView Answer on Stackoverflow
Solution 10 - JsonReinoView Answer on Stackoverflow
Solution 11 - JsonTasawar HussainView Answer on Stackoverflow