🔍 Code Extractor

function clean_for_json_v1

Maturity: 43

Recursively sanitizes nested data structures (dictionaries, lists, tuples) by converting NaN and Inf values to None and normalizing NumPy types to native Python types for JSON serialization.

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/e4e8cb00-c17d-4282-aa80-5af67f32952f/project_1/analysis.py
Lines:
364 - 382
Complexity:
moderate

Purpose

This function prepares complex nested data structures for JSON serialization by handling problematic values that are not JSON-compliant. It converts NaN and Inf float values to None, converts NumPy integer and floating types to native Python types, converts tuples to strings, and ensures dictionary keys are strings. This is essential when preparing data from scientific computing libraries (pandas, numpy) for JSON output or API responses.

Source Code

def clean_for_json(obj):
    """Recursively clean NaN and Inf values from nested dictionaries and lists"""
    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):
        return int(obj)
    elif isinstance(obj, np.floating):
        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. Can be a dictionary, list, tuple, float, NumPy integer, NumPy floating point number, or any other type. The function recursively processes nested structures.

Return Value

Returns a cleaned version of the input object with the same structure but with JSON-safe values. Specifically: dictionaries have string keys and cleaned values; lists contain cleaned elements; tuples are converted to strings; float NaN/Inf values become None; NumPy integers become Python ints; NumPy floats become Python floats (or None if NaN/Inf); 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

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

# Example usage
data = {
    'values': [1.5, float('nan'), float('inf'), np.int64(42)],
    'nested': {'key': np.float64(3.14), 'bad': float('nan')},
    'tuple_data': (1, 2, 3)
}

cleaned = clean_for_json(data)
print(cleaned)
# Output: {'values': [1.5, None, None, 42], 'nested': {'key': 3.14, 'bad': None}, 'tuple_data': '(1, 2, 3)'}

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

Best Practices

  • Use this function before calling json.dumps() on data that may contain NumPy types or NaN/Inf values
  • Be aware that tuples are converted to strings, which may not be reversible if you need to deserialize back to tuples
  • The function converts NaN and Inf to None rather than raising an error, so ensure this behavior is acceptable for your use case
  • Dictionary keys are always converted to strings, which is required for JSON but may change the original data structure
  • For large nested structures, this recursive function may be slow; consider iterative approaches for performance-critical applications
  • The function does not handle custom objects or complex types beyond the specified types; these will be returned unchanged and may still cause JSON serialization errors

Related Versions

Other versions of this component:

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v5 96.8% similar

    Recursively traverses nested data structures (dictionaries, lists) and sanitizes numeric values by converting NaN and Inf to None, and normalizing NumPy numeric types to native Python types for JSON serialization.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/e4e8cb00-c17d-4282-aa80-5af67f32952f/analysis_1.py
  • function clean_for_json_v6 96.6% similar

    Recursively traverses nested data structures (dicts, lists) and sanitizes floating-point values by replacing NaN and Inf with None, while also converting NumPy numeric types to native Python types.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/d1e252f5-950c-4ad7-b425-86b4b02c3c62/analysis_4.py
  • function clean_for_json_v2 96.2% similar

    Recursively traverses nested data structures (dicts, lists) and sanitizes numeric values by converting NaN and Inf to None, and numpy types to native Python types for JSON serialization.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/e9b7c942-87b5-4a6f-865e-e7a0d62fb0a1/analysis_2.py
  • function clean_for_json_v4 95.0% similar

    Recursively traverses nested data structures (dicts, lists, arrays) and converts NaN and Inf float values to None for safe JSON serialization, while also converting NumPy types to native Python types.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/7372154d-807e-4723-a769-4668761944b5/analysis_2.py
  • function clean_for_json_v7 93.1% similar

    Recursively traverses and sanitizes data structures (dicts, lists, numpy types) to ensure JSON serialization compatibility by converting numpy types to native Python types and handling NaN/Inf values.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/d1e252f5-950c-4ad7-b425-86b4b02c3c62/analysis_1.py
← Back to Browse