🔍 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 unparse 45.1% similar

    Converts a Python dictionary into an XML document string, serving as the reverse operation of XML parsing. Supports customizable formatting, encoding, and XML generation options.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
  • function print_status 36.6% similar

    Prints a formatted status report for SharePoint to FileCloud synchronization operations, displaying sync statistics, timing information, and health indicators.

    From: /tf/active/vicechatdev/SPFCsync/monitor.py
  • class V4JsonFormat 36.4% similar

    V4JsonFormat is a class that implements OData V4 JSON format specifications, managing metadata levels, IEEE754 compatibility, and streaming options for JSON serialization.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/odata/v4/json_format.py
  • function _emit 36.0% similar

    Recursively converts a dictionary structure into XML SAX events, emitting them through a content handler for XML generation.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
  • class JsonLightFormat 35.2% similar

    JSON Light format for SharePoint Online/One Drive for Business

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/odata/v3/json_light_format.py
← Back to Browse