Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

How to pretty print a JSON object in Python

Working with JSON (JavaScript Object Notation) data is a common task in modern programming. JSON is a lightweight data interchange format, and it is often used to store data, to specify configuration files, and for data exchange between a server and a web application. While JSON is great for its simplicity and readability, raw JSON data can be challenging to work with in Python, especially when it's not properly formatted. That's where pretty printing JSON in Python comes to the rescue.

In this blog post, we will explore how to pretty print JSON data in Python, making it more human-readable and easier to work with.

Pretty printing, in the context of JSON, refers to formatting JSON data in a way that makes it easier for humans to read and understand. By default, when you serialize a Python object to JSON using the json module, the resulting JSON data is often compact and not visually appealing. Pretty printing adds indentation and line breaks to make the JSON data more structured and readable.

Using the json Module

Python's built-in json module provides a convenient way to work with JSON data. To pretty print JSON using this module, you first import the json module and then use the json.dumps() function with the indent parameter to specify the level of indentation required.

How to pretty print a JSON object in Python

Here is a sample program:

import json
student = { "name": "Kathy", "age": 12,
    "city": "New York"}
print(json.dumps(student, indent=3))

Note that we have specified a JSON object (“student”) which is not indented in any readable manner. After we use json.dumps and print the result, we get the pretty printed result:

{
  "name": "Kathy",
  "age": 12,
  "city": "New York"
}

Here is an example of a JSON with multiple levels of nesting:

import json

data = {
  "name": "Mickey Mouse",
  "age": 15,
  "address": {
  "street": "123 Main St",
  "city": "New York",
  "zipcode": "10001"
  },
"contacts": [
  {
    "type": "email",
    "value": "mickey@example.com"
  },
  {
      "type": "phone",
      "value": "555-555-5555"
  }
]
}

indented_json = json.dumps(data, indent=2)

print(indented_json)

In this example, we have a JSON object with multiple levels of nesting, including an object (address) and an array (contacts). The indent parameter in json.dumps() is set to 2, which specifies that the JSON should be indented with two spaces for each level of nesting. When you run this code, it will produce nicely formatted JSON with indentation:

{
  "name": "Mickey Mouse",
  "age": 15,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zipcode": "10001"
  },
  "contacts": [
    {
      "type": "email",
      "value": "mickey@example.com"
    },
    {
      "type": "phone",
      "value": "555-555-5555"
    }
  ]
}

This formatted JSON is more human-readable and makes it easier to understand the structure of the data.

If you liked learning about pretty printing JSON, see our blogpost on how to bold text in Python.

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.