How To Get keys From JSON objects in Python

In this tutorial, you’ll learn various methods of extracting keys from JSON objects in Python.

We’ll cover basic techniques like using the dict.keys() method, and more advanced strategies, including dealing with nested JSON objects and handling dynamic keys.

 

 

Using dict.keys() Method

The dict.keys() method returns a view object displaying a list of all the keys in the dictionary.

Here’s how you can use the dict.keys() method to extract its keys:

network_device = {
    "device_id": "Router123",
    "location": "Data Center 1",
    "status": "active",
    "bandwidth": "1Gbps",
    "last_updated": "2023-12-27"
}
keys = network_device.keys()
print(keys)

Output:

dict_keys(['device_id', 'location', 'status', 'bandwidth', 'last_updated'])

Here, you see the keys: ‘device_id’, ‘location’, ‘status’, ‘bandwidth’, and ‘last_updated’. These keys represent the different attributes of the network device.

 

Using a Loop

Looping through the keys of a JSON object allows you to perform operations on each key separately.

Suppose you want to print each key in the JSON object using a loop:

network_device = {
    "device_id": "Router123",
    "location": "Data Center 1",
    "status": "active",
    "bandwidth": "1Gbps",
    "last_updated": "2023-12-27"
}
for key in network_device.keys():
    print(key)

Output:

device_id
location
status
bandwidth
last_updated

In this output, each line represents a key from the JSON object.

 

Using List Comprehension

You can use list comprehension to extract keys from JSON objects:

network_device = {
    "device_id": "Router123",
    "location": "Data Center 1",
    "status": "active",
    "bandwidth": "1Gbps",
    "last_updated": "2023-12-27"
}
keys = [key for key in network_device]
print(keys)

Output:

['device_id', 'location', 'status', 'bandwidth', 'last_updated']

This output is a list of keys from the network device JSON object.

The list comprehension [key for key in network_device] iterates over each key in the network_device dictionary and adds it to a new list.

 

Using Custom Functions

When handling JSON objects, you can create functions that process keys in a way that standard library functions cannot.

network_device = {
    "device_id": "Router123",
    "location": "Data Center 1",
    "status": "active",
    "bandwidth": "1Gbps",
    "last_updated": "2023-12-27"
}
def extract_keys_with_prefix(dictionary, prefix):
    return [key for key in dictionary.keys() if key.startswith(prefix)]
keys_with_l = extract_keys_with_prefix(network_device, 'l')
print(keys_with_l)

Output:

['location', 'last_updated']

In this code, the custom function extract_keys_with_prefix is designed to filter keys based on a specified prefix.

It iterates through the keys of the given dictionary and selects those that start with the provided prefix (‘l’ in this case).

 

Using Pandas

Let’s apply Pandas to our example:

import pandas as pd
network_device = {
    "device_id": "Router123",
    "location": "Data Center 1",
    "status": "active",
    "bandwidth": "1Gbps",
    "last_updated": "2023-12-27"
}
df = pd.DataFrame([network_device])
keys = df.columns.tolist()
print(keys)

Output:

['device_id', 'location', 'status', 'bandwidth', 'last_updated']

In this example, the JSON object is converted into a Pandas DataFrame, where each key becomes a column in the DataFrame.

The df.columns.tolist() method is used to extract these column names (keys of the original JSON object) and convert them into a list.

 

Extract Keys from Nested JSON Objects

Extracting keys from nested JSON objects requires recursion.

Recursion involves a function calling itself to handle data structures that have multiple levels of depth, like nested JSON objects.

Let’s explore how to implement a recursive function to extract keys from a nested JSON object:

network_data = {
    "router": {
        "device_id": "Router123",
        "status": "active",
        "interfaces": {
            "interface1": {"bandwidth": "1Gbps", "status": "up"},
            "interface2": {"bandwidth": "100Mbps", "status": "down"}
        }
    },
    "timestamp": "2023-12-27"
}
def extract_keys_recursive(obj, parent_key=''):
    keys = []
    for key, value in obj.items():
        new_key = f"{parent_key}.{key}" if parent_key else key
        keys.append(new_key)
        if isinstance(value, dict):
            keys.extend(extract_keys_recursive(value, new_key))
    return keys
keys = extract_keys_recursive(network_data)
print(keys)

Output:

['router', 'router.device_id', 'router.status', 'router.interfaces', 'router.interfaces.interface1', 'router.interfaces.interface1.bandwidth', 'router.interfaces.interface1.status', 'router.interfaces.interface2', 'router.interfaces.interface2.bandwidth', 'router.interfaces.interface2.status', 'timestamp']

In this example, the extract_keys_recursive function iterates through each key-value pair in the JSON object.

If a value is another dictionary (which means a nested structure), the function calls itself with this nested dictionary.

 

Get Keys from Second Level

Extracting keys from the second level of a JSON object involves targeting a specific nested structure within the JSON.

Consider a JSON object that contains nested objects:

network_device = {
    "device_id": "Router123",
    "details": {
        "manufacturer": "TechCorp",
        "model": "X200",
        "specifications": {
            "bandwidth": "1Gbps",
            "ports": 4
        }
    },
    "status": "active"
}
def extract_second_level_keys(obj, target_key):
    return list(obj[target_key].keys()) if target_key in obj else []
keys_in_details = extract_second_level_keys(network_device, 'details')
print(keys_in_details)

Output:

['manufacturer', 'model', 'specifications']

In this example, the function extract_second_level_keys targets the keys within the ‘details’ section of the JSON object.

 

Get Keys from JSON Objects with Dynamic Keys

Dealing with JSON objects that have dynamic keys, where the key names are not fixed.

In such cases, you need a method that can adapt to varying key names.

The idea is to focus on the structure and data types within the JSON object rather than the specific key names.

network_events = {
    "20231227_event1": {"type": "maintenance", "status": "completed"},
    "20231227_event2": {"type": "outage", "status": "resolved"},
    "20231228_event3": {"type": "upgrade", "status": "scheduled"}
}
def extract_dynamic_keys(obj):
    keys = []
    for key in obj:
        keys.append(key)
        if isinstance(obj[key], dict):
            nested_keys = [f"{key}.{nested_key}" for nested_key in obj[key]]
            keys.extend(nested_keys)
    return keys
keys = extract_dynamic_keys(network_events)
print(keys)

Output:

['20231227_event1', '20231227_event1.type', '20231227_event1.status', '20231227_event2', '20231227_event2.type', '20231227_event2.status', '20231228_event3', '20231228_event3.type', '20231228_event3.status']

In this code, the extract_dynamic_keys function iterates through the JSON object without assuming any specific key names.

It checks if a value is a dictionary (a nested JSON object) and then extracts the keys from this nested object.

Leave a Reply

Your email address will not be published. Required fields are marked *