function clean_for_json_v15
Recursively sanitizes Python objects to make them JSON-serializable by converting NumPy types to native Python types and handling NaN/Inf float values.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/analysis_3.py
392 - 407
simple
Purpose
This function prepares data structures for JSON serialization by recursively traversing dictionaries and lists, converting NumPy integer and float types to native Python types, and replacing NaN and infinite float values with None. This is essential when working with data from NumPy/Pandas that needs to be serialized to JSON format, as JSON does not support NaN, Infinity, or NumPy-specific data types.
Source Code
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, np.int64)):
return int(obj)
elif isinstance(obj, (np.floating, np.float64)):
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 for JSON serialization. Can be a dict, list, float, NumPy integer/float, or any other type. The function recursively processes nested structures (dicts and lists) and converts problematic types to JSON-compatible equivalents.
Return Value
Returns a JSON-serializable version of the input object. Dictionaries and lists are returned with all nested values cleaned. NumPy integers (np.integer, np.int64) are converted to Python int. NumPy floats (np.floating, np.float64) and Python floats are converted to Python float, except NaN and Inf values which become None. All other types are returned 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, np.int64)):
return int(obj)
elif isinstance(obj, (np.floating, np.float64)):
if math.isnan(obj) or math.isinf(obj):
return None
return float(obj)
return obj
# Example usage
data = {
'values': [np.int64(42), np.float64(3.14), float('nan'), float('inf')],
'nested': {
'numpy_int': np.int32(100),
'numpy_float': np.float32(2.718),
'bad_float': float('-inf')
}
}
cleaned_data = clean_for_json(data)
json_string = json.dumps(cleaned_data)
print(json_string)
# Output: {"values": [42, 3.14, null, null], "nested": {"numpy_int": 100, "numpy_float": 2.718, "bad_float": null}}
Best Practices
- Always call this function before using 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 modifies the structure by creating new objects; it does not modify the original object in-place
- For large nested structures, be mindful of recursion depth limits, though Python's default limit should handle most practical cases
- Consider the semantic meaning of converting NaN/Inf to None in your specific use case, as this represents missing data rather than special float values
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v13 96.5% similar
-
function clean_for_json_v14 96.4% similar
-
function clean_for_json_v12 95.2% similar
-
function clean_for_json_v11 95.1% similar
-
function clean_for_json_v9 93.3% similar