MyTetra Share
Делитесь знаниями!
Curl output to display in the readable JSON format in UNIX shell script
Время создания: 02.02.2018 11:51
Текстовые метки: curl json output format
Раздел: cURL, wget



In my UNIX shell script, when i execute a curl command, my curl result will be displayed as below which i am redirecting it 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"}


Pls suggest

json shell curl

shareimprove this question

edited Dec 1 '14 at 22:22

Gilles Quenot

84.4k17142143

asked Dec 1 '14 at 22:18

Jams

2861410

add a comment

6 Answers

active

oldest

votes

up vote

147

down vote


Try doing this :


curl ... | json_pp


or with jq using the identity filter :


curl ... | jq '.'


enter image description here


or with nodejs and bash :


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


shareimprove this answer

edited Jul 9 '17 at 3:09

Tympanix

386

answered Dec 1 '14 at 22:23

Gilles Quenot

84.4k17142143


show 9 more comments

up vote

16

down vote


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

shareimprove this answer

answered Dec 1 '14 at 22:24

0xbb

1964


add a comment

up vote

7

down vote


brew install jq

command + | jq

(example: curl localhost:5000/blocks | jq)

Enjoy!


enter image description here

shareimprove this answer

answered Sep 15 '17 at 20:39

alexanderjsingleton

342613


add a comment

up vote

4

down vote


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!

shareimprove this answer

answered Jul 20 '16 at 10:17

Raptor

1,41421623

add a comment

up vote

1

down vote


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, e.g., 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), e.g. echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp


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


shareimprove this answer

edited Jun 22 '17 at 13:23

David Brabant

23.6k95884

answered May 10 '17 at 1:27

Zhenhua

19936

add a comment

up vote

-2

down vote


If your API supports, then you just need to append ?pretty keyword to your curl command as-

If your curl command is like this-

curl -XGET 'http://localhost:9200/_cluster/state'

then to have pretty output use like this-

curl -XGET 'http://localhost:9200/_cluster/state'?pretty

shareimprove this answer

edited Jan 17 at 10:36

answered Jan 31 '17 at 12:39

Yogesh Jilhawar

576826


 
MyTetra Share v.0.53
Яндекс индекс цитирования