Looking For A Specific Value In Json File
I have a json file created by a function. The file is looks like this : { 'images': [ { 'image': '/WATSON/VISUAL-REC/../IMAGES/OBAMA.jpg', 'classifiers': [
Solution 1:
Since the "person" class is not necessarily the first in the first element of the "classes" array. you should iterate over all of the elements there.
for entry in in output_json['images'][0]['classifiers'][0]['classes']:
if ('class' in entry) and (entry['class'] == 'person'):
person += 1
print (' FUNCTION : jsonanalysis // step There is a person in this image')
return person
if you want to check if there is 'person' in a class entry in ANY of the images/classifiers, you will need to iterate over those arrays as well:
forimage_entryin output_json['images']:
forclassifier_entryin image_entry['classifiers']:
forclass_entryinin classifier_entry["classes"]:
if class_entry['class'] == 'person':
person += 1print (' FUNCTION : jsonanalysis // step There is a person in this image')return person
Note, that if you want to count the total number of 'person' occurrences, you should not return inside the loop, but only after it is completed.
Solution 2:
Try following :
1 Lets assume your data is stored into the python dict, if not try to convert it.
data_dict = {
"images": [
{
"image": "/WATSON/VISUAL-REC/../IMAGES/OBAMA.jpg",
"classifiers": [
{
"classes": [
{
"score": 0.921,
"class": "President of the United States",
"type_hierarchy": "/person/President of the United States"
},
{
"score": 0.921,
"class": "person"
},
{
"score": 0.73,
"class": "ivory color"
},
{
"score": 0.648,
"class": "Indian red color"
}
],
"classifier_id": "default",
"name": "default"
}
]
}
],
"custom_classes": 0,
"images_processed": 1
}
Convert the json to string using following: import json
data_str = json.dumps(data_dict)
Now check the word in the string (data_str):
if'person'in data_str: return True else: return False
Post a Comment for "Looking For A Specific Value In Json File"