How to Pretty Print JSON in Python
Author: Newtum
This guide demonstrates how to format and display JSON data in a more readable, "pretty" format in Python. This is especially useful for debugging and inspecting API responses.
Using `json.dumps()` with Indentation
The easiest way to pretty-print JSON is to use the `indent` and `sort_keys` arguments of the `json.dumps()` function. `indent` adds newlines and spaces, and `sort_keys` organizes the keys alphabetically.
Printing a Requests Response Prettily
When you get a JSON response from an API using the `requests` library, you can combine `response.json()` with `json.dumps()` to print it nicely.