🔍 Code Extractor

function clean_for_json_v12

Maturity: 32

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.

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/project_3/analysis.py
Lines:
409 - 428
Complexity:
moderate

Purpose

This function prepares complex Python data structures for JSON serialization by handling edge cases that would otherwise cause serialization errors. It recursively traverses nested structures (dicts, lists) and converts problematic types: NumPy integers/floats to native Python types, pandas Series/DataFrames to dictionaries, tuples to strings, and NaN/Inf float values to None. This is essential when working with scientific computing libraries and needing to export data to JSON format for APIs, file storage, or data interchange.

Source Code

def clean_for_json(obj):
    if isinstance(obj, dict):
        return {str(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, tuple):
        return str(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)
    elif isinstance(obj, (pd.Series, pd.DataFrame)):
        return clean_for_json(obj.to_dict())
    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 primitive type (int, float, str), collection (dict, list, tuple), NumPy type (np.integer, np.floating), or pandas object (pd.Series, pd.DataFrame). The function handles nested structures recursively.

Return Value

Returns a JSON-serializable version of the input object. Dictionaries have string keys and cleaned values, lists contain cleaned elements, tuples become strings, NaN/Inf floats become None, NumPy types become native Python types, pandas objects become dictionaries, and other types are returned unchanged. The return type matches the structure of the input but with all elements converted to JSON-compatible types.

Dependencies

  • math
  • numpy
  • pandas

Required Imports

import math
import numpy as np
import pandas as pd

Usage Example

import math
import numpy as np
import pandas as pd

def clean_for_json(obj):
    if isinstance(obj, dict):
        return {str(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, tuple):
        return str(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)
    elif isinstance(obj, (pd.Series, pd.DataFrame)):
        return clean_for_json(obj.to_dict())
    return obj

# Example usage
data = {
    'numpy_int': np.int64(42),
    'numpy_float': np.float64(3.14),
    'nan_value': float('nan'),
    'inf_value': float('inf'),
    'tuple': (1, 2, 3),
    'nested': {'series': pd.Series([1, 2, 3])},
    'list': [np.int32(10), float('nan')]
}

cleaned = clean_for_json(data)
import json
json_str = json.dumps(cleaned)
print(json_str)

Best Practices

  • Always use this function before calling json.dumps() on data structures containing NumPy or pandas objects
  • Be aware that tuples are converted to strings, which may lose structure information
  • NaN and Inf values are converted to None (null in JSON), ensure downstream code handles null values appropriately
  • Dictionary keys are converted to strings, so numeric keys will become string representations
  • For large pandas DataFrames, consider using orient parameter in to_dict() for better control over output format before cleaning
  • The function modifies the structure by creating new objects rather than modifying in-place, so original data remains unchanged

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v15 95.2% 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_v13 95.2% 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/d48d7789-9627-4e96-9f48-f90b687cd07d/analysis_1.py
  • function clean_for_json_v14 94.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_v3 94.5% similar

    Recursively converts Python objects (including NumPy and Pandas types) into JSON-serializable formats by handling special numeric types, NaN/Inf values, and nested data structures.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f0a78968-1d2b-4fbe-a0c6-a372da2ce2a4/project_1/analysis.py
  • function clean_for_json 94.1% similar

    Recursively traverses and sanitizes Python data structures (dicts, lists, tuples, numpy arrays) to ensure all values are JSON-serializable by converting numpy types, handling NaN/Inf values, and normalizing data types.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f0b81d95-24d9-418a-8d9f-1b241684e64c/project_1/analysis.py
← Back to Browse