function clean_for_json_v9
Recursively sanitizes Python objects (dicts, lists, floats) to ensure they are JSON-serializable by converting NaN and infinity values to None and ensuring all dictionary keys are strings.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/c385e1f5-fbf6-4832-8fd4-78ef8b72fc53/project_2/analysis.py
107 - 116
simple
Purpose
This function prepares Python data structures for JSON serialization by handling edge cases that would cause json.dumps() to fail. It recursively traverses nested dictionaries and lists, converts all dictionary keys to strings, and replaces non-JSON-compliant float values (NaN and infinity) with None. This is particularly useful when working with data from numerical libraries like NumPy or Pandas that may contain special float values.
Source Code
def clean_for_json(obj):
if isinstance(obj, dict):
return {str(k): clean_for_json(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [clean_for_json(item) for item in obj]
elif isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
return obj
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: The Python object to clean for JSON serialization. Can be a dictionary, list, float, or any other primitive type. Nested structures (dicts containing lists containing dicts, etc.) are supported and will be recursively processed.
Return Value
Returns a cleaned version of the input object that is JSON-serializable. Dictionaries have all keys converted to strings and values recursively cleaned. Lists have all elements recursively cleaned. Float values that are NaN or infinity are converted to None. All other types are returned unchanged. The return type matches the input type structure (dict returns dict, list returns list, etc.).
Dependencies
math
Required Imports
import math
Usage Example
import math
import json
def clean_for_json(obj):
if isinstance(obj, dict):
return {str(k): clean_for_json(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [clean_for_json(item) for item in obj]
elif isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
return obj
# Example usage
data = {
'name': 'test',
'values': [1.5, float('nan'), float('inf'), 3.14],
'nested': {
123: 'numeric_key',
'valid': 2.5,
'invalid': float('-inf')
}
}
cleaned = clean_for_json(data)
print(json.dumps(cleaned, indent=2))
# Output:
# {
# "name": "test",
# "values": [1.5, null, null, 3.14],
# "nested": {
# "123": "numeric_key",
# "valid": 2.5,
# "invalid": null
# }
# }
Best Practices
- Use this function before calling json.dumps() on data that may contain NaN or infinity values from numerical computations
- Be aware that NaN and infinity values are converted to None (null in JSON), which may affect downstream processing that expects numeric values
- Dictionary keys are converted to strings, so numeric keys will lose their original type (e.g., integer key 123 becomes string '123')
- The function does not handle custom objects or other non-JSON-serializable types beyond floats - these will be returned unchanged and may still cause JSON serialization errors
- Consider using this in data pipelines that export results from Pandas/NumPy computations to JSON format
- The function creates new objects rather than modifying in-place, so it's safe to use with shared data structures
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v11 95.7% similar
-
function clean_for_json_v15 93.3% similar
-
function clean_for_json_v12 92.6% similar
-
function clean_for_json_v14 92.3% similar
-
function clean_for_json_v13 91.7% similar