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
-




Have Questions? Post them here!