Technology
21 min read
5 Essential Python Functions for Effortless JSON Parsing & Processing
KDnuggets
January 19, 2026•3 days ago

AI-Generated SummaryAuto-generated
This article presents five useful Python functions for handling complex JSON data. These functions enable safe extraction of nested values, flattening of nested structures, deep merging of JSON objects, filtering JSON by schema, and conversion between JSON and dot notation. These tools address common challenges in API integration, data processing, and configuration management, offering practical solutions for developers.
Image by Author
# Introduction
Working with JSON in Python is often challenging. The basic json.loads() only gets you so far.
API responses, configuration files, and data exports often contain JSON that is messy or poorly structured. You need to flatten nested objects, safely extract values without KeyError exceptions, merge multiple JSON files, or convert between JSON and other formats. These tasks come up constantly in web scraping, API integration, and data processing. This article walks you through five practical functions for handling common JSON parsing and processing tasks.
You can find the code for these functions on GitHub.
# 1. Safely Extracting Nested Values
JSON objects often nest several levels deep. Accessing deeply nested values with bracket notation gets challenging fast. If any key is missing, you get a KeyError.
Here is a function that lets you access nested values using dot notation, with a fallback for missing keys:
Let's test it with a complex nested structure:
Output:
The function splits the path string on dots and walks through the data structure one key at a time. At each level, it checks if the current value is a dictionary or a list. For dictionaries, it uses .get(key), which returns None for missing keys instead of raising an error. For lists, it tries to convert the key to an integer index.
The default parameter provides a fallback when any part of the path does not exist. This prevents your code from crashing when dealing with incomplete or inconsistent JSON data from APIs.
This pattern is especially useful when processing API responses where some fields are optional or only present under certain conditions.
# 2. Flattening Nested JSON into Single-Level Dictionaries
Machine learning models, CSV exports, and database inserts often need flat data structures. But API responses and configuration files use nested JSON. Converting nested objects to flat key-value pairs is a common task.
Here is a function that flattens nested JSON with customizable separators:
Now let's flatten a complex nested structure:
Output:
The function uses recursion to handle arbitrary nesting depth. When it encounters a dictionary, it processes each key-value pair, building up the flattened key by concatenating parent keys with the separator.
For lists, it uses the index as part of the key. This lets you preserve the order and structure of array elements in the flattened output. The pattern reviews_0_rating tells you this is the rating from the first review.
The separator parameter lets you customize the output format. Use dots for dot notation, underscores for snake_case, or slashes for path-like keys depending on your needs.
This function is particularly useful when you need to convert JSON API responses into dataframes or CSV rows where each column needs a unique name.
# 3. Deep Merging Multiple JSON Objects
Configuration management often requires merging multiple JSON files containing default settings, environment-specific configs, user preferences, and more. A simple dict.update() only handles the top level. You need deep merging that recursively combines nested structures.
Here is a function that deep merges JSON objects:
Let's try merging sample configuration info:
Output:
The function recursively merges nested dictionaries. When both the base and override contain dictionaries at the same key, it merges those dictionaries instead of replacing them entirely. This preserves values that are not explicitly overridden.
Notice how database.port and database.timeout remain from the default configuration, while database.host gets overridden. The pool settings merge at the nested level, so min and max both get updated.
The function also adds new keys that do not exist in the base config, like the monitoring section in the production override.
You can chain multiple merges to layer configurations:
This pattern is common in application configuration where you have defaults, environment-specific settings, and runtime overrides.
# 4. Filtering JSON by Schema or Whitelist
APIs often return more data than you need. Large JSON responses make your code harder to read. Sometimes you only want specific fields, or you need to remove sensitive data before logging.
Here is a function that filters JSON to keep only specified fields:
Let's filter a sample API response:
Output:
The schema acts as a whitelist. Setting a field to True includes it in the output. Using a nested dictionary lets you filter nested objects. The function recursively applies the schema to nested structures.
For arrays, the schema applies to each item. In the example, the posts array gets filtered so each post only includes id, title, and views, while content and internal_score are excluded.
Notice how sensitive fields like password_hash and private_notes do not appear in the output. This makes the function useful for sanitizing data before logging or sending to frontend applications.
You can create different schemas for different use cases, such as a minimal schema for list views, a detailed schema for single-item views, and an admin schema that includes everything.
# 5. Converting JSON to and from Dot Notation
Some systems use flat key-value stores, but you want to work with nested JSON in your code. Converting between flat dot-notation keys and nested structures helps achieve this.
Here is a pair of functions for bidirectional conversion.
// Converting JSON to Dot Notation
// Converting Dot Notation to JSON
Let's test the round-trip conversion:
Output:
The json_to_dot_notation function flattens the structure by recursively walking through nested dictionaries and joining keys with dots. Unlike the earlier flatten function, this one does not handle arrays; it is optimized for configuration data that is purely key-value.
The dot_notation_to_json function reverses the process. It splits each key on dots and builds up the nested structure by creating intermediate dictionaries as needed. The loop handles all parts except the last one, creating nesting levels. Then it assigns the value to the final key.
This approach keeps your configuration readable and maintainable while working within the constraints of flat key-value systems.
# Wrapping Up
JSON processing goes beyond basic json.loads(). In most projects, you will need tools to navigate nested structures, transform shapes, merge configurations, filter fields, and convert between formats.
The techniques in this article transfer to other data processing tasks as well. You can modify these patterns for XML, YAML, or custom data formats.
Start with the safe access function to prevent KeyError exceptions in your code. Add the others as you run into specific needs. Happy coding!
Rate this article
Login to rate this article
Comments
Please login to comment
No comments yet. Be the first to comment!
