Jsonalias: JSON Type Alias
Jsonalias is a microlibrary (current version 0.1.2) that defines a `Json` type alias for Python. It provides a convenient way to type-hint JSON-like structures, which are typically composed of nested dictionaries, lists, strings, numbers, booleans, and None. Given its singular purpose, the release cadence is expected to be slow, with updates primarily for compatibility or if the alias is integrated into Python's standard `typing` module.
Warnings
- gotcha The library's `Json` alias explicitly mirrors Python's native JSON representation, including `None` for null. Ensure your data conversion matches this if coming from other languages.
- gotcha The author notes that this alias might eventually be added to Python's standard `typing` module. If `Json` is officially adopted there, the `jsonalias` library might become redundant or could lead to naming conflicts if not carefully managed.
Install
-
pip install jsonalias
Imports
- Json
from jsonalias import Json
Quickstart
from jsonalias import Json
# Example of type-hinting a complex JSON structure
def process_json_data(data: Json):
print(f"Processing data: {data}")
if isinstance(data, dict):
print(f"Keys: {list(data.keys())}")
elif isinstance(data, list):
print(f"Length: {len(data)}")
# A valid JSON structure adhering to the alias
data_example: Json = {
"name": "Alice",
"age": 30,
"isStudent": False,
"courses": ["Math", "Science"],
"address": {"street": "123 Main St", "zip": "90210"},
"metadata": None
}
process_json_data(data_example)
# Another example, a list of JSON objects
list_example: Json = [
{"id": 1, "value": "A"},
{"id": 2, "value": "B"}
]
process_json_data(list_example)