Reputation: 945
This is the script
def validate_record_schema(record):
device = record.get('Payload', {})
manual_added= device.get('ManualAdded', None)
location = device.get('Location', None)
if isinstance(manual_added, dict) and isinstance(location, dict):
if 'Value' in manual_added and 'Value' in location:
return False
return isinstance(manual_added, bool) and isinstance(location, str)
print([validate_record_schema(r) for r in data])
This is json data
data = [{
"Id": "12",
"Type": "DevicePropertyChangedEvent",
"Payload": [{
"DeviceType": "producttype",
"DeviceId": 2,
"IsFast": false,
"Payload": {
"DeviceInstanceId": 2,
"IsResetNeeded": false,
"ProductType": "product",
"Product": {
"Family": "home"
},
"Device": {
"DeviceFirmwareUpdate": {
"DeviceUpdateStatus": null,
"DeviceUpdateInProgress": null,
"DeviceUpdateProgress": null,
"LastDeviceUpdateId": null
},
"ManualAdded": {
"value":false
},
"Name": {
"Value": "Jigital60asew",
"IsUnique": true
},
"State": null,
"Location": {
"value":"bangalore"
},
"Serial": null,
"Version": "2.0.1.100"
}
}
}]
}]
For the line device = device.get('ManualAdded', None)
, I am getting the following error: AttributeError: 'list' object has no attribute 'get'.
please have a look and help me to solve this issue
Where i am doing mistake...
How can i fix this error?
Please help me to solve this issue
Upvotes: 5
Views: 93648
Reputation: 1702
As the error suggests, you can't .get()
on a list. To get the Location and ManualAdded field, you could use:
manual_added = record.get('Payload')[0].get('Payload').get('Device').get('ManualAdded')
location = record.get('Payload')[0].get('Payload').get('Device').get('Location')
So your function would become:
def validate_record_schema(record):
manual_added = record.get('Payload')[0].get('Payload').get('Device').get('ManualAdded')
location = record.get('Payload')[0].get('Payload').get('Device').get('Location')
if isinstance(manual_added, dict) and isinstance(location, dict):
if 'Value' in manual_added and 'Value' in location:
return False
return isinstance(manual_added, bool) and isinstance(location, str)
Note that this would set location to
{
"value":"bangalore"
}
and manual_added to
{
"value":false
}
Upvotes: 3
Reputation: 77337
You are having problems tracking types as you traverse data
. One trick is to add prints along the way for debug to see what is going on. For instance, that top "Payload" object is a list of dict
, not a single dict
. The list implies that you can have more than one device descriptor so I wrote a sample that checks all of them and returns False if it finds something wrong along the way. you will likely need to update this according to your validation rules, but this will get you started.
def validate_record_schema(record):
"""Validate that the 0 or more Payload dicts in record
use proper types"""
err_path = "root"
try:
for device in record.get('Payload', []):
payload = device.get('Payload', None)
if payload is None:
# its okay to have device without payload?
continue
device = payload["Device"]
if not isinstance(device["ManualAdded"]["value"], bool):
return False
if not isinstance(device["Location"]["value"], str):
return False
except KeyError as e:
print("missing key")
return False
return True
Upvotes: 3