How to Pretty Print JSON output with cURL

This article shows 3 methods or tools to make JSON output easier to read, or "pretty print" the JSON after receiving it with cURL requests.

Table of contents:

In the default cURL requests, the JSON output is generally in compact format:

Terminal

$ curl https://api.cloudflare.com/client/v4/       

{"success":false,"errors":[{"code":7000,"message":"No route for that URI"}]}

1. Pretty print JSON with jq

The jq is a popular JSON tool in the Unix/Linux environment, also described as "sed for JSON data."

Terminal

$ curl -s https://api.cloudflare.com/client/v4/ | jq .

{
  "success": false,
  "errors": [
    {
      "code": 7000,
      "message": "No route for that URI"
    }
  ],
  "messages": [],
  "result": null
}

If we don’t have jq installed, try installing it using package managers:

  • On Ubuntu/Debian – sudo apt install jq
  • On CentOS/RHEL – sudo yum install jq
  • On macOS – brew install jq

P.S The -s option in cURL means silent or quiet mode; it makes curl hide the progress meters and error messages that are normally in output.

2. Pretty print JSON with json_pp

This json_pp is typically pre-installed on the Unix/Linux environment. It stands for JSON pretty print. It is less powerful than jq, but it does the job perfectly.

Terminal

curl -s https://api.cloudflare.com/client/v4/ | json_pp

{
   "errors" : [
      {
         "code" : 7000,
         "message" : "No route for that URI"
      }
   ],
   "messages" : [],
   "result" : null,
   "success" : false
}

3. Pretty print JSON with Python

If we have Python installed, we can use the Python’s built-in JSON tool python -m json.tool or python3 -m json.tool to format or pretty print JSON output.

Terminal

curl -s https://api.cloudflare.com/client/v4/ | python3 -m json.tool

{
    "success": false,
    "errors": [
        {
            "code": 7000,
            "message": "No route for that URI"
        }
    ],
    "messages": [],
    "result": null
}

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nabil
2 years ago

json_pp for the win. Thanks mkyong.

Bob
3 years ago

Well, you made my day. I have been playing around with Python modules to do this because I did not know about json_pp. Thanks so much sir. Have a great day.

B

tottran
2 months ago

Thank you Mkyong!

Abdal Asif
1 year ago

Thanks mkyong