How to Pretty Print cURL JSON Output in Terminal


In order to pretty print the cURL output which is in JSON format you can make use of the Python json module.


Example:

% curl -s https://randomuser.me/api/ | python3 -m json.tool

{
    "results": [
        {
            "gender": "male",
            "name": {
                "title": "Mr",
                "first": "Akhil",
                "last": "Rai"
            },
            "location": {
                "street": {
                    "number": 7324,
                    "name": "Coaker's Walk"
                },
                "city": "Thrissur",
                "state": "Rajasthan",
                "country": "India",
                "postcode": 41746,
                "coordinates": {
                    "latitude": "8.2937",
                    "longitude": "-5.5920"
                },
                "timezone": {
                    "offset": "-6:00",
                    "description": "Central Time (US & Canada), Mexico City"
                }
            } ...

Pretty Print cURL JSON Output using Python json tool

This does format the JSON output but does not add color coding, for this, you can download packages from Brew such as jsonpp or jq

Example:

% brew install jq
% curl -s https://randomuser.me/api/ | jq
cURL JSON Output Pretty Printed with Colors

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap