function clean_for_json_v6
Recursively traverses nested data structures (dicts, lists) and sanitizes floating-point values by replacing NaN and Inf with None, while also converting NumPy numeric types to native Python types.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/d1e252f5-950c-4ad7-b425-86b4b02c3c62/analysis_4.py
307 - 323
simple
Purpose
This function prepares complex nested data structures for JSON serialization by handling problematic values that are not JSON-compliant. It converts NaN (Not a Number) and Inf (Infinity) values to None, and converts NumPy integer and floating-point types to native Python int and float types. This is essential when preparing data from scientific computing libraries (like pandas/numpy) for JSON export or API responses.
Source Code
def clean_for_json(obj):
"""Recursively clean NaN and Inf values from nested structures"""
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)
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 nested structures like dictionaries, lists, floats, NumPy numeric types, or any other primitive type. The function recursively processes nested dictionaries and lists.
Return Value
Returns a cleaned version of the input object with the same structure. Float values that were NaN or Inf are replaced with None. NumPy integer types (np.integer) are converted to Python int. NumPy floating types (np.floating) are converted to Python float (or None if NaN/Inf). All other types are returned unchanged. The return type matches the input structure (dict returns dict, list returns list, etc.).
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 {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)
return obj
# Example usage
data = {
'values': [1.5, float('nan'), float('inf'), np.float64(3.14)],
'counts': [np.int64(10), np.int32(20)],
'nested': {
'bad_value': float('nan'),
'good_value': 42
}
}
cleaned = clean_for_json(data)
print(cleaned)
# Output: {'values': [1.5, None, None, 3.14], 'counts': [10, 20], 'nested': {'bad_value': None, 'good_value': 42}}
import json
json_string = json.dumps(cleaned)
print(json_string)
Best Practices
- Use this function before calling json.dumps() on data that may contain NumPy types or NaN/Inf values
- Be aware that NaN and Inf values are converted to None (null in JSON), which may affect downstream data analysis
- This function creates a new cleaned copy of the data structure rather than modifying in-place
- The function handles arbitrary nesting depth, but very deep structures may cause recursion limits
- Consider the semantic meaning of replacing NaN/Inf with None in your specific use case - sometimes preserving these as strings might be more appropriate
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v2 97.5% similar
-
function clean_for_json_v5 97.5% similar
-
function clean_for_json_v4 96.7% similar
-
function clean_for_json_v1 96.6% similar
-
function clean_for_json_v11 93.4% similar