function print_json
A utility function that prints a Python dictionary as formatted JSON with 2-space indentation to standard output.
/tf/active/vicechatdev/test_acl_functions.py
21 - 23
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
-
function test_json_serialization 50.7% similar
-
function clean_for_json_v9 49.8% similar
-
function clean_for_json 48.6% similar
-
function clean_for_json_v12 48.0% similar