How do I pretty print JSON output in curl?

When I curl my API endpoint, the output is not readable on the terminal and I’m having to take the extra steps of copy and paste in online formatting tools to read it. Is there a way to make the output readable in the terminal?

$ curl -X https://localhost:8080/api/v1/auth/1
[{"_id":"612bf1b534ea97204aa3c68e","index":0,"guid":"b3fe7289-9aca-4add-baf6-12ff710c7d24","isActive":false,"balance":"$2,516.45","picture":"http://placehold.it/32x32","age":24}]
  1. Accepted Answer - curl ... | json_pp (JSON Pretty Printer)

Accepted Answer - curl ... | json_pp (JSON Pretty Printer)

json_pp is a utility for pretty printing JSON input. In other words, you give it compact JSON as input and it outputs the same JSON, pretty printed. It generally comes pre-installed on macOS, but if not you can install it easily.

In your example

$ curl -X https://localhost:8080/api/v1/auth/1 | json_pp
[
   {
      "_id" : "612bf1b534ea97204aa3c68e",
      "age" : 24,
      "balance" : "$2,516.45",
      "guid" : "b3fe7289-9aca-4add-baf6-12ff710c7d24",
      "index" : 0,
      "isActive" : false,
      "picture" : "http://placehold.it/32x32"
   }
]

Note, you can pass input JSON input to json_pp, here’s how I got your JSON output to pretty print.

% echo '[{"_id":"612bf1b534ea97204aa3c68e","index":0,"guid":"b3fe7289-9aca-4add-baf6-12ff710c7d24","isActive":false,"balance":"$2,516.45","picture":"http://placehold.it/32x32","age":24}]' | json_pp
[
   {
      "_id" : "612bf1b534ea97204aa3c68e",
      "age" : 24,
      "balance" : "$2,516.45",
      "guid" : "b3fe7289-9aca-4add-baf6-12ff710c7d24",
      "index" : 0,
      "isActive" : false,
      "picture" : "http://placehold.it/32x32"
   }
]

Speak Your Mind