How to flatten a nested JSON object in Python?

Published on Aug. 22, 2023, 12:17 p.m.

Flattening a nested JSON object in Python means converting a JSON object that has nested objects and arrays into a simpler key-value format where all values are strings. There are several approaches to achieve this functionality in Python, including writing a custom recursive flattening function or using third-party libraries.

One popular library that can flatten nested JSON objects is json_flatten. Here’s an example of how to use json_flatten:

import json
from json_flatten import flatten

# Example nested JSON object
nested_obj = {
    "name": "John",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    },
    "phone_numbers": [
        {
            "type": "home",
            "number": "555-1234"
        },
        {
            "type": "work",
            "number": "555-5678"
        }
    ]
}

# Flatten the nested object
flat_obj = flatten(nested_obj, separator=".")

# Convert the flattened object back to JSON format
json_obj = json.dumps(flat_obj)

# Print the flattened JSON object
print(json_obj)

In this example, we have a nested JSON object that we want to flatten. We use the flatten() function from the json_flatten library to flatten the object, specifying a separator character to use between keys. The resulting flattened object is then converted back into JSON format using json.dumps(), and printed to the console.

Another library that can perform this flattening is Pandas library in Python.

Pandas has the json_normalize() function, which can be used to normalize nested JSON data into a flat table. Here’s an example of how to use json_normalize() to flatten a nested JSON object in Python:

import json
import pandas as pd

# Example nested JSON object
nested_obj = {
    "name": "John",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    },
    "phone_numbers": [
        {
            "type": "home",
            "number": "555-1234"
        },
        {
            "type": "work",
            "number": "555-5678"
        }
    ]
}

# Flatten the nested object using Pandas
df = pd.json_normalize(nested_obj, sep=".")

# Convert the flattened object back to JSON format
json_obj = df.to_json(orient="records")

# Print the flattened JSON object
print(json_obj)

In this example, we have a nested JSON object that we want to flatten. We use the json_normalize() function from the Pandas library to flatten the object, specifying a separator character to use between keys. The resulting flattened object is then converted back into JSON format using the to_json() method on the flattened Pandas DataFrame, and printed to the console.

Note that you can also use the orient parameter of the to_json() method to specify the format of the resulting JSON object (e.g. “records”, “split”, “index”, etc.).

Tags: