🔍 Code Extractor

function safe_json_dumps

Maturity: 45

Safely serializes Python objects to JSON format, handling NaN values and datetime objects that would otherwise cause serialization errors.

File:
/tf/active/vicechatdev/full_smartstat/services.py
Lines:
65 - 74
Complexity:
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

  • json
  • numpy
  • pandas

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v15 75.7% similar

    Recursively sanitizes Python objects to make them JSON-serializable by converting NumPy types to native Python types and handling NaN/Inf float values.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/analysis_3.py
  • function clean_for_json_v14 74.8% similar

    Recursively sanitizes Python objects to make them JSON-serializable by converting NumPy types to native Python types and handling NaN/Inf values.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/42b81361-ba7e-4d79-9598-3090af68384b/analysis_2.py
  • function clean_for_json_v12 74.7% similar

    Recursively sanitizes Python objects to make them JSON-serializable by converting non-serializable types (NumPy types, pandas objects, tuples, NaN/Inf values) into JSON-compatible formats.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/project_3/analysis.py
  • function clean_for_json_v9 74.5% similar

    Recursively sanitizes Python objects (dicts, lists, floats) to ensure they are JSON-serializable by converting NaN and infinity values to None and ensuring all dictionary keys are strings.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/c385e1f5-fbf6-4832-8fd4-78ef8b72fc53/project_2/analysis.py
  • function clean_for_json_v11 73.9% similar

    Recursively sanitizes Python objects (dicts, lists, floats) to make them JSON-serializable by replacing NaN and infinity float values with None.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f5da873e-41e6-4f34-b3e4-f7443d4d213b/analysis_4.py
← Back to Browse