18

I am trying to do this:

curl localhost:3000/api/items/15/tools

This is the return right now:

{"recipe_tools":[{"id":19,"name":"Cutting Board","prices":{"display":"49ドル.99","value":"49.99"},"description":"Built to last,
... (and then like a million lines)

And it keeps going...

How do I pretty print or awesome print this? What can I do?

asked May 5, 2016 at 20:37
5
  • 1
    Within curl? You can't; it doesn't. There are plenty of tools you could pipe some JSON into and format it though. As such, I would suggest this is actually a software recommendation request. Commented May 5, 2016 at 20:40
  • You learn how to parse the object that is returned. Commented May 5, 2016 at 20:40
  • Possible duplicate of How can I pretty-print JSON from the command line? (from a file) Commented May 5, 2016 at 20:41
  • 1
    What's your scripting language? Commented May 5, 2016 at 20:59
  • Does this answer your question? How can I pretty-print JSON in a shell script? Commented Feb 16, 2023 at 22:14

4 Answers 4

34

There are things you can install, but the simplest way is to use Python as it's typically already installed.

curl localhost:3000/api/items/15/tools | python -m json.tool
ggorlen
59.1k8 gold badges118 silver badges173 bronze badges
answered Oct 19, 2018 at 18:31

Comments

7

I use jq to pretty-print it.

jq is a lightweight and flexible command-line JSON processor.

jq is written in portable C, and it has zero runtime dependencies.

E.g.

Without JSON formatter

$ curl -s http://localhost:3000; echo
{"account":{"id":"HXRC","uri":["mailto:[email protected]","tel:366-643-7219"],"features":["585"],"nickname":"Consultant"}}

Install jq(Ubuntu):

$ sudo apt-get install jq

Use jq json formatter.

$ curl -s http://localhost:3000 | jq '.'
{
 "account": {
 "id": "HXRC",
 "uri": [
 "mailto:[email protected]",
 "tel:366-643-7219"
 ],
 "features": [
 "585"
 ],
 "nickname": "Consultant"
 }
}

You can find more examples in tutorial. You can find the installation for each OS in download page

answered Jun 24, 2023 at 5:25

1 Comment

jq is also available through Homebrew, so brew install jq
2

Port 3000 makes me think you are using Rails.

I have used JSON.pretty_generate(json_obj) on my RoR applications to have a JSON object return properly indented.

ggorlen
59.1k8 gold badges118 silver badges173 bronze badges
answered May 5, 2016 at 21:00

Comments

1

On macOS, use json_pp

Example:

curl your_request_url | json_pp
answered Sep 3, 2024 at 21:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.