Jsonalias: JSON Type Alias

0.1.2 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

The `Json` type alias can be used to annotate variables, function parameters, and return types that are expected to conform to a JSON-like structure. It supports nested dictionaries, lists, and primitive JSON types.

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)

view raw JSON →