🔍 Code Extractor

function clean_for_json_v1

Maturity: 44

Recursively traverses nested data structures (dicts, lists) and replaces NaN and Infinity float values with None to ensure JSON serialization compatibility.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
290 - 304
Complexity:
simple

Purpose

This utility function sanitizes Python data structures before JSON serialization by handling special float values (NaN, Infinity, -Infinity) that are not valid in JSON specification. It's commonly used when preparing data from numerical computations, pandas DataFrames, or scientific libraries for API responses or file storage in JSON format.

Source Code

def clean_for_json(obj):
    """
    Recursively clean NaN and Infinity values from data structures.
    Replaces them with None to ensure valid JSON output.
    """
    import math
    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
    return obj

Parameters

Name Type Default Kind
obj - - positional_or_keyword

Parameter Details

obj: Any Python object to be cleaned. Can be a primitive type (int, float, str, bool, None), a dictionary, a list, or nested combinations thereof. The function will recursively process nested structures to find and replace invalid float values.

Return Value

Returns a cleaned version of the input object with the same structure. All NaN and Infinity float values are replaced with None. Other data types and valid float values are preserved unchanged. Return type matches input type (dict returns dict, list returns list, etc.).

Dependencies

  • math

Required Imports

import math

Usage Example

import math
import json

def clean_for_json(obj):
    import math
    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
    return obj

# Example usage
data = {
    'values': [1.5, float('nan'), 3.7, float('inf')],
    'nested': {
        'score': float('-inf'),
        'valid': 42.0
    },
    'name': 'test'
}

cleaned_data = clean_for_json(data)
print(json.dumps(cleaned_data))  # Output: {"values": [1.5, null, 3.7, null], "nested": {"score": null, "valid": 42.0}, "name": "test"}

Best Practices

  • Call this function on data structures before using json.dumps() or jsonify() when the data may contain NaN or Infinity values from numerical computations
  • This function creates new objects rather than modifying in-place, so it's safe to use with shared data structures
  • The function imports math internally, which is redundant if already imported at module level but ensures it works as a standalone function
  • Consider using this when working with pandas DataFrames converted to dictionaries, as they often contain NaN values
  • The function preserves the structure of nested data, making it safe for complex API responses
  • Note that None is used as replacement, which serializes to null in JSON - ensure your API consumers can handle null values appropriately

Related Versions

Other versions of this component:

  • clean_for_json_v1

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/e4e8cb00-c17d-4282-aa80-5af67f32952f/project_1/analysis.py | Maturity: N/A

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v11 92.4% 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_v4 91.7% 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_v6 91.0% 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 90.0% 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_v5 88.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
← Back to Browse