🔍 Code Extractor

function clean_for_json_v14

Maturity: 30

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

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/42b81361-ba7e-4d79-9598-3090af68384b/analysis_2.py
Lines:
612 - 629
Complexity:
moderate

Purpose

This function prepares complex Python objects (including nested dictionaries, lists, and NumPy arrays) for JSON serialization by converting NumPy data types to native Python types and replacing non-JSON-compliant float values (NaN and Infinity) with None. It's particularly useful when working with data science libraries that produce NumPy types which cannot be directly serialized to JSON.

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
    elif isinstance(obj, np.integer):
        return int(obj)
    elif isinstance(obj, np.floating):
        if math.isnan(obj) or math.isinf(obj):
            return None
        return float(obj)
    elif isinstance(obj, np.ndarray):
        return clean_for_json(obj.tolist())
    return obj

Parameters

Name Type Default Kind
obj - - positional_or_keyword

Parameter Details

obj: Any Python object to be cleaned for JSON serialization. Can be a primitive type (int, float, string), NumPy type (np.integer, np.floating, np.ndarray), or nested structure (dict, list). The function recursively processes nested structures.

Return Value

Returns a JSON-serializable version of the input object. NumPy integers are converted to Python int, NumPy floats to Python float, NumPy arrays to lists, and NaN/Inf values to None. Dictionaries and lists are recursively processed with the same transformations applied to their contents. Other types are returned unchanged.

Dependencies

  • numpy
  • math

Required Imports

import numpy as np
import math

Usage Example

import numpy as np
import math

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
    elif isinstance(obj, np.integer):
        return int(obj)
    elif isinstance(obj, np.floating):
        if math.isnan(obj) or math.isinf(obj):
            return None
        return float(obj)
    elif isinstance(obj, np.ndarray):
        return clean_for_json(obj.tolist())
    return obj

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

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

Best Practices

  • Always use this function before calling json.dumps() on data that may contain NumPy types
  • Be aware that NaN and Infinity values are converted to None, which may affect downstream processing
  • The function modifies the structure by converting NumPy arrays to lists, which may impact memory usage for large arrays
  • For very deeply nested structures, be mindful of potential recursion depth limits
  • This function does not handle custom objects or other non-standard types - they are returned unchanged and may still cause JSON serialization errors

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v15 96.4% 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.7% 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_v12 94.8% 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_v3 94.6% 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_v11 93.5% 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