mardi 20 novembre 2018

Robot Framework - TypeError: string indices must be integers When Parsing Json

I am trying to parse an array of json, But I am getting the following error

TypeError: string indices must be integers

Json:

{
    'locationId': 'location1',
    'name': 'Name',
    'type': 'Ward',
    'patientId': None,
    'children': [{
        'locationId': 'location2',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': [{
            'locationId': 'location3',
            'name': 'Name',
            'type': 'HospitalGroup',
            'patientId': None,
            'children': None
        }]
    }, {
        'locationId': 'location4',
        'name': 'Name',
        'type': 'Hospital',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location5',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location6',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location27',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }]
}

I am trying to the get all the locationId values and store the values one by one inside a list.

Here is what I am doing

    :FOR  ${item}  IN   @{Location_Json_List}
    \  ${locationId}=  set variable  ${item['locationId']}
    \  log  ${locationId}
    \  append to list  ${locationIds}  '${locationId}

I have also tried this

    :FOR  ${item}  IN   @{Location_Json_List}
    \  ${locationId}=  set variable  ${item['children'][0]['locationId']}
    \  log  ${locationId}
    \  append to list  ${locationIds}  '${locationId}

But I am getting the same error.

The test is failing on this line ${locationId}= set variable ${item['locationId']}

Any help would be apprectiated.

Aucun commentaire:

Enregistrer un commentaire