function clean_for_json_v11
Recursively sanitizes Python objects (dicts, lists, floats) to make them JSON-serializable by replacing NaN and infinity float values with None.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/f5da873e-41e6-4f34-b3e4-f7443d4d213b/analysis_4.py
403 - 412
simple
Purpose
This function prepares Python data structures for JSON serialization by recursively traversing nested dictionaries and lists, and replacing problematic float values (NaN and infinity) that are not valid in JSON with None. This is essential when working with numerical data from libraries like NumPy or Pandas that may contain NaN or infinite values, ensuring the data can be safely serialized to JSON format without raising exceptions.
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
return obj
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: The Python object to clean. Can be a dictionary, list, float, or any other type. Dictionaries and lists are recursively processed, floats are checked for NaN/infinity values, and all other types are returned unchanged.
Return Value
Returns a cleaned version of the input object with the same structure. For dictionaries, returns a new dict with cleaned values. For lists, returns a new list with cleaned elements. For floats that are NaN or infinity, returns None. For all other types (including valid floats), returns the original value unchanged.
Dependencies
math
Required Imports
import math
Usage Example
import math
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
return obj
# Example usage
data = {
'values': [1.5, float('nan'), 3.7, float('inf')],
'nested': {
'score': float('nan'),
'count': 42
},
'name': 'test'
}
cleaned_data = clean_for_json(data)
print(json.dumps(cleaned_data, indent=2))
# Output:
# {
# "values": [1.5, null, 3.7, null],
# "nested": {
# "score": null,
# "count": 42
# },
# "name": "test"
# }
Best Practices
- Use this function before calling json.dumps() on data that may contain NaN or infinity values from numerical computations
- The function creates new objects rather than modifying in-place, so the original data remains unchanged
- Works with nested structures of arbitrary depth
- Does not handle NumPy arrays or Pandas DataFrames directly - convert them to native Python types first
- Consider using this as a preprocessing step in data pipelines that export to JSON format
- The function returns None for invalid floats, which serializes to null in JSON - ensure downstream consumers can handle null values
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v9 95.7% similar
-
function clean_for_json_v15 95.1% similar
-
function clean_for_json_v13 94.0% similar
-
function clean_for_json_v14 93.5% similar
-
function clean_for_json_v2 93.4% similar