function clean_for_json_v10
Recursively converts Python objects containing NumPy and Pandas data types into JSON-serializable native Python types.
/tf/active/vicechatdev/vice_ai/smartstat_scripts/c385e1f5-fbf6-4832-8fd4-78ef8b72fc53/project_1/analysis.py
630 - 651
moderate
Purpose
This function sanitizes complex Python objects (dictionaries, lists, NumPy arrays, Pandas data types) to make them JSON-serializable. It handles edge cases like NaN/Inf values, tuple keys in dictionaries, and various NumPy integer/float types. This is essential when preparing data for JSON export, API responses, or storage in JSON-based databases.
Source Code
def clean_for_json(obj):
if isinstance(obj, dict):
cleaned = {}
for k, v in obj.items():
# Convert tuple keys to strings
if isinstance(k, tuple):
k = str(k)
cleaned[k] = clean_for_json(v)
return cleaned
elif isinstance(obj, list):
return [clean_for_json(item) for item in obj]
elif isinstance(obj, (np.integer, np.int64, np.int32)):
return int(obj)
elif isinstance(obj, (np.floating, np.float64, np.float32)):
if math.isnan(obj) or math.isinf(obj):
return None
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif pd.isna(obj):
return None
return obj
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: Any Python object that needs to be cleaned for JSON serialization. Can be a dict, list, NumPy array, NumPy scalar (integer/float), Pandas NA value, or any primitive type. The function recursively processes nested structures.
Return Value
Returns a JSON-serializable version of the input object. Dictionaries have tuple keys converted to strings and values recursively cleaned. Lists have all items recursively cleaned. NumPy integers/floats are converted to Python int/float. NumPy arrays are converted to lists. NaN and Inf values become None. Pandas NA values become None. Primitive types are returned unchanged.
Dependencies
numpypandasmath
Required Imports
import numpy as np
import pandas as pd
import math
Usage Example
import numpy as np
import pandas as pd
import math
import json
def clean_for_json(obj):
if isinstance(obj, dict):
cleaned = {}
for k, v in obj.items():
if isinstance(k, tuple):
k = str(k)
cleaned[k] = clean_for_json(v)
return cleaned
elif isinstance(obj, list):
return [clean_for_json(item) for item in obj]
elif isinstance(obj, (np.integer, np.int64, np.int32)):
return int(obj)
elif isinstance(obj, (np.floating, np.float64, np.float32)):
if math.isnan(obj) or math.isinf(obj):
return None
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif pd.isna(obj):
return None
return obj
# Example usage
data = {
'array': np.array([1, 2, 3]),
'int': np.int64(42),
'float': np.float32(3.14),
'nan': np.nan,
'inf': np.inf,
'pd_na': pd.NA,
(1, 2): 'tuple_key',
'nested': {'inner': np.array([4, 5])}
}
cleaned = clean_for_json(data)
print(json.dumps(cleaned, indent=2))
Best Practices
- Always use this function before calling json.dumps() on data containing NumPy or Pandas types to avoid serialization errors
- Be aware that NaN and Inf values are converted to None (null in JSON), which may affect downstream data analysis
- Tuple keys in dictionaries are converted to string representations, which cannot be automatically converted back to tuples when deserializing
- The function modifies the structure by converting NumPy arrays to lists, which may impact memory usage for large arrays
- For very large nested structures, consider the recursion depth limit (Python default is ~1000 levels)
- This function does not handle custom objects or other non-standard types - they 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_v3 93.8% similar
-
function clean_for_json_v12 93.7% similar
-
function clean_for_json_v14 91.6% similar
-
function clean_for_json_v7 91.4% similar
-
function clean_for_json_v13 91.0% similar