16

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?

Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • 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. – Sobrique May 05 '16 at 20:40
  • You learn how to parse the object that is returned. – durbnpoisn May 05 '16 at 20:40
  • Possible duplicate of [How can I pretty-print JSON from the command line? (from a file)](http://stackoverflow.com/questions/20265439/how-can-i-pretty-print-json-from-the-command-line-from-a-file) – dafyk May 05 '16 at 20:41
  • 1
    What's your scripting language? – Mojtaba May 05 '16 at 20:59
  • Does this answer your question? [How can I pretty-print JSON in a shell script?](https://stackoverflow.com/questions/352098/how-can-i-pretty-print-json-in-a-shell-script) – ggorlen Feb 16 '23 at 22:14

3 Answers3

27

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
  • 44,755
  • 7
  • 76
  • 106
Grimnoff
  • 438
  • 4
  • 9
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
  • 44,755
  • 7
  • 76
  • 106
1

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:Lizzie.Koepp@hotmail.com","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:Lizzie.Koepp@hotmail.com",
      "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

Lin Du
  • 88,126
  • 95
  • 281
  • 483