I'm using Python to convert a json file to more human readable output, and a specific entry in that json can take one of the following formats. I'm trying create a method that handles creating appropriate output without checking for type.
"responseType": null
or
"responseType": "FEATURE MODEL"
or
"responseType": {
"type": "array",
"of": "Feature"
}
or
"responseType": {
"type": "array",
"of": {
"type": "number",
"format": "int32"
}
}
My desired output in each case is something like:
The responseType is null
The responseType is a Feature Model
The responseType is an array of Feature
The responseType is an array of int32 numbers
I'm new to Python and my first inclination is to do a lot of type checking and string concatenation. i.e.:
str = "The response type is "
if type(obj["responseType"]) is str:
str += obj["responseType"]
elif type(obj["responseType"]) is dict:
str += obj["responseType"]["type"] + " "
if type(obj["responseType"]["of"] is str
str += obj["responseType"]["of"]
else:
#dict output
#etc...
elif type(obj["responseType"] is None:
print("The response type is null")
Doing this feels very naive and incorrect, given that I repeatedly read that you should never check for type.
So, what is the pythonic way to handle this case without doing all that type checking?