Here are a few examples of pretty printing json from a command line terminal. As you can see in the example output, rich
console can output json with proper indention and color syntax highlighting.
from rich.console import Console
console = Console ()
json = """{"User": { "name": "John Doe", "email": "johndoe@example.com", "age": 32,
"gender": "Male", "isActive": true, "isAdmin":false}}"""
console . print_json ( json )
PrettyPrint JSON
import urllib
from rich.console import Console
console = Console ()
url = "https://en.wikipedia.org/w/api.php?action=query&origin=*&format=json&generator=search&gsrnamespace=0&gsrlimit=5&gsrsearch=%27Python%27"
resp = urllib . get ( url )
console . print_json ( resp )
https: //en.wikipedia.org/w/api.php?action=query&origin=*&format=json&generator=search&gsrnamespace=0&gsrlimit=5&gsrsearch=%27Python%27
{
"batchcomplete" : "" ,
"continue" : {
"gsroffset" : 5 ,
"continue" : "gsroffset||"
},
"query" : {
"pages" : {
"271890" : {
"pageid" : 271890 ,
"ns" : 0 ,
"title" : "Ball python" ,
"index" : 4
},
"21356332" : {
"pageid" : 21356332 ,
"ns" : 0 ,
"title" : "History of Python" ,
"index" : 5
},
"18942" : {
"pageid" : 18942 ,
"ns" : 0 ,
"title" : "Monty Python" ,
"index" : 3
},
"46332325" : {
"pageid" : 46332325 ,
"ns" : 0 ,
"title" : "Python" ,
"index" : 2
},
"23862" : {
"pageid" : 23862 ,
"ns" : 0 ,
"title" : "Python (programming language)" ,
"index" : 1
}
}
}
}
PrettyPrint JSON response from API
rich - Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal