function clean_for_json_v1
Recursively sanitizes nested data structures (dictionaries, lists, tuples) by converting NaN and Inf values to None and normalizing NumPy types to native Python types for JSON serialization.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/e4e8cb00-c17d-4282-aa80-5af67f32952f/project_1/analysis.py
364 - 382
moderate
Purpose
This function prepares complex nested data structures for JSON serialization by handling problematic values that are not JSON-compliant. It converts NaN and Inf float values to None, converts NumPy integer and floating types to native Python types, converts tuples to strings, and ensures dictionary keys are strings. This is essential when preparing data from scientific computing libraries (pandas, numpy) for JSON output or API responses.
Source Code
def clean_for_json(obj):
"""Recursively clean NaN and Inf values from nested dictionaries and lists"""
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, tuple):
return str(obj)
elif isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
if math.isnan(obj) or math.isinf(obj):
return None
return float(obj)
return obj
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: Any Python object to be cleaned. Can be a dictionary, list, tuple, float, NumPy integer, NumPy floating point number, or any other type. The function recursively processes nested structures.
Return Value
Returns a cleaned version of the input object with the same structure but with JSON-safe values. Specifically: dictionaries have string keys and cleaned values; lists contain cleaned elements; tuples are converted to strings; float NaN/Inf values become None; NumPy integers become Python ints; NumPy floats become Python floats (or None if NaN/Inf); all other types are returned unchanged.
Dependencies
mathnumpy
Required Imports
import math
import numpy as np
Usage Example
import math
import numpy as np
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, tuple):
return str(obj)
elif isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
if math.isnan(obj) or math.isinf(obj):
return None
return float(obj)
return obj
# Example usage
data = {
'values': [1.5, float('nan'), float('inf'), np.int64(42)],
'nested': {'key': np.float64(3.14), 'bad': float('nan')},
'tuple_data': (1, 2, 3)
}
cleaned = clean_for_json(data)
print(cleaned)
# Output: {'values': [1.5, None, None, 42], 'nested': {'key': 3.14, 'bad': None}, 'tuple_data': '(1, 2, 3)'}
import json
json_str = json.dumps(cleaned)
print(json_str)
Best Practices
- Use this function before calling json.dumps() on data that may contain NumPy types or NaN/Inf values
- Be aware that tuples are converted to strings, which may not be reversible if you need to deserialize back to tuples
- The function converts NaN and Inf to None rather than raising an error, so ensure this behavior is acceptable for your use case
- Dictionary keys are always converted to strings, which is required for JSON but may change the original data structure
- For large nested structures, this recursive function may be slow; consider iterative approaches for performance-critical applications
- The function does not handle custom objects or complex types beyond the specified types; these will be returned unchanged and may still cause JSON serialization errors
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v5 96.8% similar
-
function clean_for_json_v6 96.6% similar
-
function clean_for_json_v2 96.2% similar
-
function clean_for_json_v4 95.0% similar
-
function clean_for_json_v7 93.1% similar