Skip to content

Pretty Printing on the Terminal⚓︎

Background⚓︎

Historically I have always pretty printed json API responses by piping them to python. Like so:

curl -s 'https://dummyjson.com/products?limit=1' | python3 -m json.tool

This gives a nicely formatted response like the below:

{
    "products": [
        {
            "id": 1,
            "title": "iPhone 9",
            "description": "An apple mobile which is nothing like apple",
            "price": 549,
            "rating": 4.69,
            "brand": "Apple",
            "category": "smartphones",
        }
    ]
}

Enter Rich⚓︎

With Rich you are now able to pretty print json API outputs in COLOUR like so:

curl -s 'https://dummyjson.com/products?limit=1' | rich - --json --style="on grey100" -a heavy

Which provides the below output which is even nicer than just pretty printed json.

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 {                                                                           
   "products": [                                                             
     {                                                                       
       "id": 1,                                                              
       "title": "iPhone 9",                                                  
       "description": "An apple mobile which is nothing like apple",         
       "price": 549,                                                         
       "discountPercentage": 12.96,                                          
       "rating": 4.69,                                                       
       "stock": 94,                                                          
       "brand": "Apple",                                                     
       "category": "smartphones",                                            
       ]                                                                     
     }                                                                       
 }                                                                           
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛