🔍 Code Extractor

function print_json

Maturity: 39

A utility function that prints a Python dictionary as formatted JSON with 2-space indentation to standard output.

File:
/tf/active/vicechatdev/test_acl_functions.py
Lines:
21 - 23
Complexity:
simple

Purpose

This function provides a convenient way to display dictionary data in a human-readable JSON format. It's commonly used for debugging, logging, or displaying API responses and structured data in a pretty-printed format. The function wraps Python's json.dumps() with consistent formatting settings (2-space indentation) to ensure uniform output across an application.

Source Code

def print_json(data: Dict[str, Any]) -> None:
    """Print formatted JSON data."""
    print(json.dumps(data, indent=2))

Parameters

Name Type Default Kind
data Dict[str, Any] - positional_or_keyword

Parameter Details

data: A dictionary containing any JSON-serializable data types (strings, numbers, booleans, lists, nested dictionaries, None). The dictionary keys must be strings, and values must be JSON-serializable. Non-serializable objects (like custom classes, functions, or file handles) will raise a TypeError.

Return Value

Type: None

Returns None. This function has a side effect of printing the formatted JSON string to standard output (stdout). The output is the JSON representation of the input dictionary with 2-space indentation for readability.

Dependencies

  • json

Required Imports

import json
from typing import Dict
from typing import Any

Usage Example

import json
from typing import Dict, Any

def print_json(data: Dict[str, Any]) -> None:
    """Print formatted JSON data."""
    print(json.dumps(data, indent=2))

# Example usage
user_data = {
    "name": "John Doe",
    "age": 30,
    "email": "john@example.com",
    "active": True,
    "roles": ["admin", "user"],
    "metadata": {
        "last_login": "2024-01-15",
        "login_count": 42
    }
}

print_json(user_data)
# Output:
# {
#   "name": "John Doe",
#   "age": 30,
#   "email": "john@example.com",
#   "active": true,
#   "roles": [
#     "admin",
#     "user"
#   ],
#   "metadata": {
#     "last_login": "2024-01-15",
#     "login_count": 42
#   }
# }

Best Practices

  • Ensure the input dictionary contains only JSON-serializable data types to avoid TypeError exceptions
  • Be cautious when printing large dictionaries as they may produce extensive output that clutters the console
  • Consider using logging instead of print() for production applications to allow better control over output destinations and levels
  • For sensitive data (passwords, API keys, tokens), sanitize the dictionary before printing to avoid exposing secrets in logs or console output
  • If you need to capture the formatted JSON string instead of printing it, use json.dumps(data, indent=2) directly
  • The function uses the default JSON encoder; custom objects will need to implement __dict__ or provide a custom encoder via json.dumps()

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function to_json 62.6% similar

    Converts a Python object to a formatted JSON string with 2-space indentation, using a safe serialization helper function.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function test_json_serialization 50.7% similar

    A test function that validates JSON serialization and deserialization of workflow data structures containing status, progress, and results information.

    From: /tf/active/vicechatdev/full_smartstat/test_enhanced_progress.py
  • function clean_for_json_v9 49.8% 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 48.6% similar

    Recursively traverses and sanitizes Python data structures (dicts, lists, tuples, numpy arrays) to ensure all values are JSON-serializable by converting numpy types, handling NaN/Inf values, and normalizing data types.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f0b81d95-24d9-418a-8d9f-1b241684e64c/project_1/analysis.py
  • function clean_for_json_v12 48.0% 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
← Back to Browse