Skip to content

JSON

To use JSON inputs use dict as Argument type

it will do something like

import json

data = json.loads(user_input)

Usage

You will get all the correct editor support, attributes, methods, etc for the dict object:

import json

import typer
from typing_extensions import Annotated


def main(user_info: Annotated[dict, typer.Option()]):
    print(f"User Info: {json.dumps(user_info)}")


if __name__ == "__main__":
    typer.run(main)

Check it:

// Run your program
$ python main.py --user-info '{"name": "Camila", "age": 15, "height_meters": 1.7, "female": true}'

User Info: {"name": "Camila", "age": 15, "height_meters": 1.7, "female": true}