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?
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"
}
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
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.
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
cat myFile.json | python -mjson.tool >> myFilePretty.json
then opened in my editor (vim, don't hate) and got colors.
You can use the json node module:
npm i -g json
then simply append | json
after curl. curl http://localhost:8880/test.json | json
json
module, you could use it directly with npx
: curl http://localhost:8880/test.json | npx json
python -m json.tool
Curl http://127.0.0.1:5000/people/api.json | python -m json.tool
can also help.
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
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!
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
}
A lot more features (slice, filter and map and transform structured ) apart from formatting.
https://stedolan.github.io/jq/
Success story sharing
json_pp
seems to format data in reverse order than it's expected. Is there a way to un-reverse data?