I have a json file that looks something like this:
{
"mAutomaticTestCompleted": true,
"mAutomaticTestList": [
{
"mName": "acceleratorEntity",
"mTestStatus": true,
"mX": 3.8043518,
"mY": 8.114105,
"mZ": -3.3895721
},
{
"mName": "barometerEntity",
"mTestStatus": false,
"mValue": 0
}]
}
There are actually lots of fields like mAutomaticTestlist
, all of them are lists of objects that look just like that.
I need to write a function that takes device_name
and JSON itself as its arguments and returns the value of the mTestStatus
field.
Here's my attempt:
def hasPassed(device_name, data):
if isinstance(data, dict):
for key, value in data.items():
if not isinstance(value, dict) and not isinstance(value, list):
if key == 'mName' and value == device_name:
return data['mTestStatus']
else:
return hasPassed(device_name, value)
elif isinstance(data, list):
for element in data:
return hasPassed(device_name, element)
The problem with this function is that it doesn't go over the whole JSON object.
EDIT:
So I would like my function to work this way:
hasPassed('barometerEntity', json_obj)
would return False
cos that's the value of the 'mTestStatus' corresponding the device_name (which is barometerEntity in this case).