0

JSON data:

new_dto_data = [{'DeviceInstanceId': 24, 'IsResetNeeded': False, 'ProductType': 'SLDW', 'Product': {'Family': 'SL DW'}, 'Device': {'DeviceFirmwareUpdate': {'DeviceUpdateStatus': None, 'DeviceUpdateInProgress': None, 'DeviceUpdateProgress': None, 'LastDeviceUpdateId': None}, 'ManualAdded': False, 'Name': {'DeviceName': 'SLDW25', 'IsUnique': False}, 'State': 0, 'Location': 'Room'}]

My script:

def test():
    """Validate that the 0 or more Payload dicts in record
    use proper types"""
    err_path = "root"
    try:
        for record in new_dto_data:
            device = record["Device"]
        key_data = ((device["ManualAdded"], bool), (device["State"], int),(device["Location"], (str, type(None)))
        for i in key_data:
            if not isinstance(*i):
                return False
    except KeyError as err_path:
        print("missing key")
        return False
    return True

print(test())

If I get response "False" then I should print which key it got response "False".

Script should not stop if I get response "False". It should run until end of the loop and print all keys which got "False" response.

Currently I am getting response "False" but not able to find out which key and script is stopping once I get "False".

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nrs
  • 937
  • 3
  • 17
  • 42
  • Did you mean for the `for i in key_data` loop to apply to the last entry in `new_dto_data` only? Or should that loop be indented to be part of the `for record in new_dto_data` loop? – Martijn Pieters Apr 03 '18 at 07:02
  • Also, do you understand what the `key_data` sequence does? You need to change that sequence to list *just the keys*, and not the values, at which point you have the key to print when your `isinstance()` test fails. Please don't just blindly copy and paste code from other places without understanding what that code *does*. – Martijn Pieters Apr 03 '18 at 07:03
  • How to change that sequence? I should get for which key i got "False" response – nrs Apr 03 '18 at 07:04
  • Can u help me ? – nrs Apr 03 '18 at 07:09
  • Please don't repeat questions, that's considered noise. Your previous attempt at least had the indentation right. – Martijn Pieters Apr 03 '18 at 07:10
  • Ok sure...Sorry for that...I need to get help but am not getting so thats why i asked – nrs Apr 03 '18 at 07:11
  • i will delete the question.. – nrs Apr 03 '18 at 07:12

0 Answers0