function safe_json_dumps
Safely serializes Python objects to JSON format, handling NaN values and datetime objects that would otherwise cause serialization errors.
/tf/active/vicechatdev/full_smartstat/services.py
65 - 74
moderate
Purpose
This function provides a robust JSON serialization wrapper that addresses common serialization issues. It pre-processes objects to remove NaN values (which are not valid in JSON), handles datetime objects by converting them to ISO format strings, and provides a fallback string conversion for other non-serializable objects. This is particularly useful when working with data from pandas DataFrames or numpy arrays that may contain NaN values, or when dealing with datetime objects that need to be serialized.
Source Code
def safe_json_dumps(obj, **kwargs):
"""Safely serialize objects to JSON, handling NaN values properly"""
def json_serializer(obj):
if hasattr(obj, 'isoformat'): # datetime objects
return obj.isoformat()
return str(obj)
# Pre-clean the object to remove NaN values
cleaned_obj = clean_nan_for_json(obj)
return json.dumps(cleaned_obj, default=json_serializer, **kwargs)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
**kwargs |
- | - | var_keyword |
Parameter Details
obj: The Python object to serialize to JSON. Can be any JSON-serializable type (dict, list, string, number, boolean, None) or objects containing datetime instances or NaN values that will be handled specially. Common inputs include dictionaries, lists, pandas DataFrames converted to dicts, or nested data structures.
**kwargs: Additional keyword arguments that are passed directly to json.dumps(). Common options include 'indent' for pretty-printing (e.g., indent=2), 'sort_keys' for sorted output (sort_keys=True), 'ensure_ascii' for Unicode handling (ensure_ascii=False), and 'separators' for custom formatting.
Return Value
Returns a JSON-formatted string representation of the input object. NaN values will have been replaced (likely with None or removed, depending on the clean_nan_for_json implementation), datetime objects will be converted to ISO 8601 format strings (e.g., '2023-10-15T14:30:00'), and any other non-serializable objects will be converted to their string representation. The return type is str.
Dependencies
jsonnumpypandas
Required Imports
import json
import numpy as np
import pandas as pd
Usage Example
import json
import numpy as np
import pandas as pd
from datetime import datetime
# Assuming clean_nan_for_json is defined
def clean_nan_for_json(obj):
if isinstance(obj, dict):
return {k: clean_nan_for_json(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [clean_nan_for_json(item) for item in obj]
elif isinstance(obj, float) and (np.isnan(obj) or np.isinf(obj)):
return None
return obj
def safe_json_dumps(obj, **kwargs):
def json_serializer(obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
return str(obj)
cleaned_obj = clean_nan_for_json(obj)
return json.dumps(cleaned_obj, default=json_serializer, **kwargs)
# Example usage
data = {
'name': 'Test',
'value': np.nan,
'timestamp': datetime(2023, 10, 15, 14, 30),
'scores': [1.5, np.nan, 3.7],
'nested': {'count': 42, 'invalid': float('inf')}
}
json_string = safe_json_dumps(data, indent=2)
print(json_string)
# Output will have NaN/inf replaced with null, datetime as ISO string
Best Practices
- Ensure the clean_nan_for_json() helper function is available in scope before calling this function
- Use the indent parameter for human-readable output during debugging (e.g., indent=2)
- Be aware that NaN values will be converted (typically to None/null), which may affect data interpretation
- Datetime objects will be converted to ISO 8601 strings, so deserialization will require parsing them back
- For large datasets, consider the performance impact of the pre-cleaning step
- The default json_serializer converts unknown objects to strings as a fallback, which may not always be desirable for round-trip serialization
- When working with pandas DataFrames, consider using df.to_dict() before passing to this function
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function clean_for_json_v15 75.7% similar
-
function clean_for_json_v14 74.8% similar
-
function clean_for_json_v12 74.7% similar
-
function clean_for_json_v9 74.5% similar
-
function clean_for_json_v11 73.9% similar