🔍 Code Extractor

function clean_for_json_v9

Maturity: 32

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.

File:
/tf/active/vicechatdev/vice_ai/smartstat_scripts/c385e1f5-fbf6-4832-8fd4-78ef8b72fc53/project_2/analysis.py
Lines:
107 - 116
Complexity:
simple

Purpose

This function prepares Python data structures for JSON serialization by handling edge cases that would cause json.dumps() to fail. It recursively traverses nested dictionaries and lists, converts all dictionary keys to strings, and replaces non-JSON-compliant float values (NaN and infinity) with None. This is particularly useful when working with data from numerical libraries like NumPy or Pandas that may contain special float values.

Source Code

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, 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 for JSON serialization. Can be a dictionary, list, float, or any other primitive type. Nested structures (dicts containing lists containing dicts, etc.) are supported and will be recursively processed.

Return Value

Returns a cleaned version of the input object that is JSON-serializable. Dictionaries have all keys converted to strings and values recursively cleaned. Lists have all elements recursively cleaned. Float values that are NaN or infinity are converted to None. All other types are returned unchanged. The return type matches the input type structure (dict returns dict, list returns list, etc.).

Dependencies

  • math

Required Imports

import math

Usage Example

import math
import json

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

# Example usage
data = {
    'name': 'test',
    'values': [1.5, float('nan'), float('inf'), 3.14],
    'nested': {
        123: 'numeric_key',
        'valid': 2.5,
        'invalid': float('-inf')
    }
}

cleaned = clean_for_json(data)
print(json.dumps(cleaned, indent=2))
# Output:
# {
#   "name": "test",
#   "values": [1.5, null, null, 3.14],
#   "nested": {
#     "123": "numeric_key",
#     "valid": 2.5,
#     "invalid": null
#   }
# }

Best Practices

  • Use this function before calling json.dumps() on data that may contain NaN or infinity values from numerical computations
  • Be aware that NaN and infinity values are converted to None (null in JSON), which may affect downstream processing that expects numeric values
  • Dictionary keys are converted to strings, so numeric keys will lose their original type (e.g., integer key 123 becomes string '123')
  • The function does not handle custom objects or other non-JSON-serializable types beyond floats - these will be returned unchanged and may still cause JSON serialization errors
  • Consider using this in data pipelines that export results from Pandas/NumPy computations to JSON format
  • The function creates new objects rather than modifying in-place, so it's safe to use with shared data structures

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function clean_for_json_v11 95.7% 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_v15 93.3% 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_v12 92.6% 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_v14 92.3% 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_v13 91.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
← Back to Browse