🔍 Code Extractor

function clean_for_json_v15

Maturity: 30

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

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/analysis_3.py
Lines:
392 - 407
Complexity:
simple

Purpose

This function prepares data structures for JSON serialization by recursively traversing dictionaries and lists, converting NumPy integer and float types to native Python types, and replacing NaN and infinite float values with None. This is essential when working with data from NumPy/Pandas that needs to be serialized to JSON format, as JSON does not support NaN, Infinity, or NumPy-specific data types.

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, 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)
    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 dict, list, float, NumPy integer/float, or any other type. The function recursively processes nested structures (dicts and lists) and converts problematic types to JSON-compatible equivalents.

Return Value

Returns a JSON-serializable version of the input object. Dictionaries and lists are returned with all nested values cleaned. NumPy integers (np.integer, np.int64) are converted to Python int. NumPy floats (np.floating, np.float64) and Python floats are converted to Python float, except NaN and Inf values which become None. All other types are returned unchanged.

Dependencies

  • math
  • numpy

Required Imports

import math
import numpy as np

Usage Example

import math
import numpy as np
import json

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, 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)
    return obj

# Example usage
data = {
    'values': [np.int64(42), np.float64(3.14), float('nan'), float('inf')],
    'nested': {
        'numpy_int': np.int32(100),
        'numpy_float': np.float32(2.718),
        'bad_float': float('-inf')
    }
}

cleaned_data = clean_for_json(data)
json_string = json.dumps(cleaned_data)
print(json_string)
# Output: {"values": [42, 3.14, null, null], "nested": {"numpy_int": 100, "numpy_float": 2.718, "bad_float": null}}

Best Practices

  • Always call this function before using json.dumps() on data that may contain NumPy types or NaN/Inf values
  • Be aware that NaN and Inf values are converted to None (null in JSON), which may affect downstream data analysis
  • This function modifies the structure by creating new objects; it does not modify the original object in-place
  • For large nested structures, be mindful of recursion depth limits, though Python's default limit should handle most practical cases
  • Consider the semantic meaning of converting NaN/Inf to None in your specific use case, as this represents missing data rather than special float values

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v13 96.5% 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 96.4% 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 95.2% 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_v11 95.1% 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
  • function clean_for_json_v9 93.3% 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
← Back to Browse