Python

Hi, I am new to python and working on the dictionary. can anyone guide me on how to access visibilites from below code :

dev1 = {
“status”: “OK”,
“data”: {
“results”: [
{
“tradeRelCode”: “ZT55”,
“customerCode”: “ZC0”,
“customerName”: “XYZ”,
“supplierCode”: “ZC0000816”,
“supplierName”: “abc Systems Co.,Ltd”,
“type”: “DIRECT”,
“startDate”: “2017-04-13T00:00:00.000+0000”,
“customerRef”: “null1”,
“status”: “REJECTED”,
“supplierIsMajor”: “null1”,
“shareCorporateSAQ”: “null1”,
“shareEnvironmentalSurvey”: “null1”,
“initiatedOn”: “2017-04-13T14:33:21.891+0000”,
“tier”: “null1”,
“visibilites”: [
{
“code”: “ZS0004207”,
“name”: “Aabc Systems Co.,Ltd”,
“siteVisibilityMap”: {
“SAQ”: 0,
“DETAILS”: 0,
“CONTACTS”: 0,
“SUPPLEMENTAL_SURVEYS”: 0,
“AUDIT”: 0
},
“customerRef”: “null1”
}
]
}
],
“pageNumber”: 3,
“limit”: 1,
“total”: 186
}
}

dev1[‘data’][‘results’][‘visibilities’] - I am getting error. please help 

1 Like

Hi @biswa0808,

Welcome to the Github Community Forum!

Let’s first beautify the code you send:

dev1 = {
    "status": "OK",
    "data": {
        "results": [{
            "tradeRelCode": "ZT55",
            "customerCode": "ZC0",
            "customerName": "XYZ",
            "supplierCode": "ZC0000816",
            "supplierName": "abc Systems Co.,Ltd",
            "type": "DIRECT",
            "startDate": "2017-04-13T00:00:00.000+0000",
            "customerRef": "null1",
            "status": "REJECTED",
            "supplierIsMajor": "null1",
            "shareCorporateSAQ": "null1",
            "shareEnvironmentalSurvey": "null1",
            "initiatedOn": "2017-04-13T14:33:21.891+0000",
            "tier": "null1",
            "visibilites": [{
                "code": "ZS0004207",
                "name": "Aabc Systems Co.,Ltd",
                "siteVisibilityMap": {
                    "SAQ": 0,
                    "DETAILS": 0,
                    "CONTACTS": 0,
                    "SUPPLEMENTAL_SURVEYS": 0,
                    "AUDIT": 0
                },
                "customerRef": "null1"
                }
            ]}],
        "pageNumber": 3,
        "limit": 1,
        "total": 186
    }
}

Now we have a better view of what is this json body. First of all the visibilities element have a typo error, you miss a i after the t you need to change from _ visibilites _ to visibilities, but this is not a problem.

In order to get the access to visibilities you need to retrive data, then the array results and than you can use the visibilities array :

dev1['data']['results']**[0]**['visibilities']

Note that results is an array so it wants the index of the element you want to read, maybe you didn’t notice because it have only one element, but even if it have one element remains an array.

Same think for the array visibilities, if you want the name you need to refer to it in this way:

dev1['data']['results'][0]['visibilites']**[0]**['name']

Let me know if this solve your problem :slight_smile:

-Gabriele-

Mark helpfull posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!

2 Likes

You tried to access the element like this

dev1['data']['results']['visibilities']

and the error you got maybe this

TypeError: list indices must be integers, not str

If you notice the results in the dictionary dev1 stores a list.and you tried to access a list with str,thats why you got this error.You need to acees this list first,Here is correct way to do it

dev1['data']['results'][0]['visibilities']