This has been updated to clarify the question so it can better help others in the future.
I am attempting to use an if statement to test if the key classes
exists in a JSON structure using Python. to check if a key exists in a JSON structure using Python. I am able to get the key but need help finding out what the condition should be to check if it exists.
I successfully was able to return the value of the key class
when it is exsits in the JSON structure using the following code:
#Parameter: json_data - The JSON structure it is checking
def parse_classes(json_data):
lst = list()
if('classes' is in json_data['images'][0]['classifiers'][0]): #This doesn't work
for item in json_data['images'][0]['classifiers'][0]['classes']:
lst.append(item['class'])
else:
lst = None
return(lst)
Example json_data
:
{"images": ["classifiers": ["classes": ["class": "street"]]]}
My issue seems to be on line 4 and the issue seems to be that the conditional statement is incorrect. What do I need to change about the if-statement to make it work correctly?