function clean_for_json_v4
Recursively traverses nested data structures (dicts, lists, arrays) and converts NaN and Inf float values to None for safe JSON serialization, while also converting NumPy types to native Python types.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/7372154d-807e-4723-a769-4668761944b5/analysis_2.py
431 - 449
moderate
Purpose
This function prepares data structures containing numeric values for JSON serialization by handling problematic values that are not valid in JSON format. It converts NaN (Not a Number) and Inf (Infinity) values to None, converts NumPy integer and floating types to native Python int and float, and converts NumPy arrays to lists. This is essential when preparing data from scientific computing libraries (pandas, numpy) for API responses, file storage, or any JSON-based data exchange.
Source Code
def clean_for_json(obj):
"""Recursively clean NaN and Inf values for JSON serialization"""
if isinstance(obj, dict):
return {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
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)
elif isinstance(obj, np.ndarray):
return clean_for_json(obj.tolist())
return obj
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: The input object to be cleaned. Can be any Python type including dict, list, float, int, NumPy types (np.integer, np.floating, np.ndarray), or nested combinations of these. The function recursively processes nested structures to clean all numeric values within.
Return Value
Returns a cleaned version of the input object with the same structure but with all NaN and Inf values replaced with None, NumPy integers converted to Python int, NumPy floats converted to Python float, and NumPy arrays converted to lists. For dictionaries, returns a new dict with cleaned values. For lists, returns a new list with cleaned elements. For other types that don't need cleaning, returns the original object unchanged.
Dependencies
mathnumpy
Required Imports
import math
import numpy as np
Usage Example
import math
import numpy as np
import json
def clean_for_json(obj):
if isinstance(obj, dict):
return {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
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)
elif isinstance(obj, np.ndarray):
return clean_for_json(obj.tolist())
return obj
# Example usage
data = {
'values': [1.5, float('nan'), float('inf'), -float('inf')],
'numpy_array': np.array([1, 2, 3]),
'numpy_float': np.float64(3.14),
'nested': {
'bad_value': float('nan'),
'good_value': 42
}
}
cleaned_data = clean_for_json(data)
print(json.dumps(cleaned_data, indent=2))
# Output:
# {
# "values": [1.5, null, null, null],
# "numpy_array": [1, 2, 3],
# "numpy_float": 3.14,
# "nested": {
# "bad_value": null,
# "good_value": 42
# }
# }
Best Practices
- Always use this function before calling json.dumps() on data that may contain NumPy types or NaN/Inf values to avoid JSONDecodeError
- Be aware that NaN and Inf values are converted to None (null in JSON), which may affect downstream data analysis if null handling is not implemented
- This function creates new objects rather than modifying in-place, so it's safe to use on shared data structures but may use additional memory for large datasets
- For very large nested structures, consider the recursion depth limit (default 1000 in Python) and potential stack overflow
- The function preserves the structure of the input, so dict keys and list ordering remain unchanged
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v2 97.1% similar
-
function clean_for_json_v6 96.7% similar
-
function clean_for_json_v5 96.1% similar
-
function clean_for_json_v1 95.0% similar
-
function clean_for_json_v15 92.4% similar