S
S
sswwssww2021-02-07 14:54:34
Python
sswwssww, 2021-02-07 14:54:34

How to handle inputs of different types in json?

There are a number of data sources. Each of which "fills" the values ​​of the variables in its own way. I would like to process values ​​in a unified way, but at the same time send back data based on the specifics of the received values.
Those. may come from one source {"some_value": True} and from another {"some_value": "true"}.
In my code, I want to do something like this:

if data.get("some_value"):
    return Response({"some_response": <сюда вставлять True или  "true", в зависимости от типа some_value>})

How can this be done without explicit type checking in "client code"? I think Pydantic has a solution to the problem but hasn't found it yet. Or perhaps you should create your own class with an overridden __bool__? What is the best solution you recommend?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question