ChatGPT解决这个技术问题 Extra ChatGPT

Display curl output in readable JSON format in Unix shell script

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?


N
Nikita Fedyashev

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 :

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

with and :

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

is this the way ?curl -i -vs -X POST -H "$SESSION_TOKEN" -H "$AUTH_TOKEN" -H "Accept:$ACCEPT_HEADER" -H "Content-Type:text/plain" "http://$BASE_URI/api" | json_pp >> jsonoutput.json
Absolutely. Have you tested ?
yes, but nothing been written to the file "jsonoutput.json". Its size is zero.
json_pp seems to format data in reverse order than it's expected. Is there a way to un-reverse data?
json_pp is a Perl command utility which converts between some input and output formats (one of them JSON). The default input format is json and the default output format is json with pretty option.
K
Koray Tugay

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

Thanks for answering. But i am not using Python, im using shell. can u guide me on shell
@Jams that executes python from the shell giving your a pretty printed output.
Great solution as python comes preinstalled.
That python script gives a "broken pipe" error when I add a " | head" to it.
@CrouchingKitten I don't see that problem myself (maybe different versions), but that may be due to the head command cutting off the output being piped to it before reaching the end, thus "breaking" the pipe. If you allow head to consume the entire data stream from the pipe, I'd expect the error to not show up.
Z
Zack

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

Thanks @zhenhua! I used cat myFile.json | python -mjson.tool >> myFilePretty.json then opened in my editor (vim, don't hate) and got colors.
M
Michael Johansen

You can use the json node module:

npm i -g json

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


I would remove the comment and part about sudo if you don't recommend it haha. I agree it's not necessary. Thanks for the answer
If you don't want to install the json module, you could use it directly with npx: curl http://localhost:8880/test.json | npx json
this should be the accepted response IMO, this is the easiest way to go. no options required, outputs exactly what you'd expect
V
Vega
python -m json.tool
Curl http://127.0.0.1:5000/people/api.json | python -m json.tool

can also help.


A
Arefe

You can install jq and make the query like below:

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

https://i.stack.imgur.com/VZDEd.png


why do I get command not found when I use it like this but not when I just run jq without anything else?
@AhmedHasn. What OS do you use? Anyways, irrespective of that, you will need to install the jq and it should work.
RHEL7 but as I said jq is installed and it works if I don't use it in a pipe
Sorry I am not sure what's the issue then.
R
Raptor

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!


If using Ubuntu you can find json_reformat in the yajl-tools package.
Z
Zack

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
}

m
mitnk

Check out curljson

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

R
Reino

With :

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

xidel can probably retrieve the JSON for you as well.


T
Tasawar Hussain

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

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