How to validate in python if key has value? I have this:
"version3.0" : {
"customizationInfo" : {
"surfaces" : [ {
"name" : "Custom Name",
"areas" : [ {
"customizationType" : "TextPrinting",
"colorName" : "black",
"fill" : "#000",
"fontFamily" : "Red",
"Dimensions" : {
"width" : 367,
"height" : 94
},
"Position" : {
"x" : 14,
"y" : 96
},
"name" : "Name Requested",
"label" : "Demo label",
"text" : "Need To Validate This"
} ]
}, {
"name" : "Gift Message Options",
"areas" : [ {
"customizationType" : "Options",
"name" : "Gift Message Options",
"label" : "Gift Message Options",
"optionValue" : ""
} ]
}, {
"name" : "Name of Gift Giver",
"areas" : [ {
"customizationType" : "TextPrinting",
"colorName" : "black",
"fill" : "#000",
"fontFamily" : "Green",
"Dimensions" : {
"width" : 380,
"height" : 151
},
"Position" : {
"x" : 12,
"y" : 158
},
"name" : "Name of Gift Giver",
"label" : "Name of Gift Giver",
"text" : ""
} ]
} ]
}
}
I'm trying to get
custom_name = json_file['version3.0']['customizationInfo']['surfaces'][0]['areas'][0]['text']
If this field is not empty it works fine, if empty is not. I tried a lot of ways to validate, but as i'm not familiar with Python too close i can't make correct validation. This doesn't works:
if json_file['version3.0']['customizationInfo']['surfaces'][0]['areas'][0]['text']:
custom_name = json_file['version3.0']['customizationInfo']['surfaces'][0]['areas'][0]['text']
else:
custom_name = 'empty'
Gives error: KeyError('text',))
Python. How to check if values exists at json?