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?

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
Sign up to request clarification or add additional context in comments.

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

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.

Comments

1

On macOS, use json_pp

Example:

curl your_request_url | json_pp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.