🔍 Code Extractor

function clean_for_json_v11

Maturity: 32

Recursively sanitizes Python objects (dicts, lists, floats) to make them JSON-serializable by replacing NaN and infinity float values with None.

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/f5da873e-41e6-4f34-b3e4-f7443d4d213b/analysis_4.py
Lines:
403 - 412
Complexity:
simple

Purpose

This function prepares Python data structures for JSON serialization by recursively traversing nested dictionaries and lists, and replacing problematic float values (NaN and infinity) that are not valid in JSON with None. This is essential when working with numerical data from libraries like NumPy or Pandas that may contain NaN or infinite values, ensuring the data can be safely serialized to JSON format without raising exceptions.

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

Parameters

Name Type Default Kind
obj - - positional_or_keyword

Parameter Details

obj: The Python object to clean. Can be a dictionary, list, float, or any other type. Dictionaries and lists are recursively processed, floats are checked for NaN/infinity values, and all other types are returned unchanged.

Return Value

Returns a cleaned version of the input object with the same structure. For dictionaries, returns a new dict with cleaned values. For lists, returns a new list with cleaned elements. For floats that are NaN or infinity, returns None. For all other types (including valid floats), returns the original value unchanged.

Dependencies

  • math

Required Imports

import math

Usage Example

import math
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
    return obj

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

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

Best Practices

  • Use this function before calling json.dumps() on data that may contain NaN or infinity values from numerical computations
  • The function creates new objects rather than modifying in-place, so the original data remains unchanged
  • Works with nested structures of arbitrary depth
  • Does not handle NumPy arrays or Pandas DataFrames directly - convert them to native Python types first
  • Consider using this as a preprocessing step in data pipelines that export to JSON format
  • The function returns None for invalid floats, which serializes to null in JSON - ensure downstream consumers can handle null values

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v9 95.7% 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
  • function clean_for_json_v15 95.1% 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 94.0% 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 93.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/42b81361-ba7e-4d79-9598-3090af68384b/analysis_2.py
  • function clean_for_json_v2 93.4% 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
← Back to Browse