0

I am trying to see if any JSON object (with keys and values) is an instance of an interface implemented.

I have done the following thing:

JSON Data:

_json = {"action": "hello", "completed_at": "2020-04-08T00:38:23+05:30", "created_at": "2020-04-08T00:38:23+05:30",
         "group_id": "1234", "request_id": "6543",
         "result": {"match_output": {"name_on_card": 2},
                    "source_output": {"first_name": "abc", "gender": "m", "id_number": "4fk5fk",
                                      "last_name": "xyz", "middle_name": "", "name_on_card": "abc xyz",
                                      "source": "hello", "status": "id_found"}}, "status": "completed",
         "task_id": "1234", "type": "xxx"}


from typing_extensions import TypedDict


class Match(TypedDict):
    name_on_card: str


class Source(TypedDict):
    first_name: str
    gender: str
    id_number: str
    last_name: str
    middle_name: str
    name_on_card: str
    source: str


class Result:
    match_output: Match
    source_output: Source


class CallbackJSON(Result):
    action: str
    completed_at: str
    created_at: str
    group_id: str
    request_id: str
    result: Result
    status: str
    task_id: str
    type: str

I just want below statement or something like this to be true:

print(isinstance(_json, type(CallbackJSON)))
Neeraj Sonaniya
  • 375
  • 4
  • 13
  • "any JSON object " => there's no such thing as a "JSON object" in Python. Json is a text format, what you have here is just a plain Python dict. Also (unrelated but) you'd want `isinstance(_json, CallbackJson)` - `type(CallbackJson)` returns the metaclass, not what you want here. – bruno desthuilliers Apr 08 '20 at 07:59
  • Is there anyway we could get that, any other method? Interface match with dict structure? – Neeraj Sonaniya Apr 08 '20 at 08:23
  • Looks like you want [json schema](https://json-schema.org/). – bruno desthuilliers Apr 08 '20 at 09:24

0 Answers0