# Tried to access key from dictionary and facing an issue

**URL:** <https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [June 1, 2022, 10:58am UTC](https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246 "2022-06-01T10:58:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gunpathe](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@gunpathe](https://forum.inductiveautomation.com/u/gunpathe)\
**Post date:** [June 1, 2022, 10:58am UTC](https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246/1 "2022-06-01T10:58:46Z")

</div>

Hi Guys,  
I have a JSON response which is =  
{  
“columns”:[  
{  
“name”:“user\_id”,  
“type”:“java.lang.Integer”  
},  
{  
“name”:“contact\_type”,  
“type”:“java.lang.String”  
},  
{  
“name”:“contact\_value”,  
“type”:“java.lang.String”  
}  
],  
“rows”:[  
[  
1,  
“email”,  
“[susan.richards@mailinator.com](mailto:susan.richards@mailinator.com)”  
],  
[  
3,  
“email”,  
“[jane.doe@mailinator.com](mailto:jane.doe@mailinator.com)”  
]  
]  
}

Created a dictionary and passed this json response. Then, when i try to get the “rows” key it says ,  
**TypeError: unicode indices must be integers**

This is my code:  
import json

ds = system.db.runNamedQuery(“FetchUsers”)  
pds = system.dataset.toPyDataSet(ds)  
jsonString = system.util.jsonEncode(pds)  
#print(jsonString)  
dump = json.dumps(jsonString)  
res = json.loads(dump)  
print(res[“rows”])

By the way, it works well in online python console.  
please do needful.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 1, 2022, 11:37am UTC](https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246/2 "2022-06-01T11:37:08Z")

</div>

with proper formatting:

```json
{
   "columns":[
      {
         "name":"user_id",
         "type":"java.lang.Integer"
      },
      {
         "name":"contact_type",
         "type":"java.lang.String"
      },
      {
         "name":"contact_value",
         "type":"java.lang.String"
      }
   ],
   "rows":[
      [
         1,
         "email",
         "susan.richards@mailinator.com"
      ],
      [
         3,
         "email",
         "jane.doe@mailinator.com"
      ]
   ]
}

```

You’re mixing up two json processing libraries, and making them do the same thing twice.  
`json.dumps()` is meant to convert a dict into its string representation, but here you’re passing it a string.  
I’m not quite sure what’s happening with the `json.loads()` at this point, but it can’t be good ! It seems it’s returning a string as well, so when you’re trying to index it it doesn’t work.  
Remove the `dump = json.dumps(jsonString)` line and pass your `jsonString` to `loads` and you should be able to index it properly.

But more importantly, what are you trying to do ? Where is that used ? I kind of feel there ought to be a better way than transforming a dataset into a pydataset, then into a jsonstring, then into a dict.

---

<div class="post-metadata">

**Author:** ![gunpathe](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@gunpathe](https://forum.inductiveautomation.com/u/gunpathe)\
**Post date:** [June 1, 2022, 12:32pm UTC](https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246/3 "2022-06-01T12:32:51Z")

</div>

My mistake,  
It worked Thanks.  
By the way am using this for Alarm pipeline to dynamically fetch user info from DB

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 1, 2022, 12:36pm UTC](https://forum.inductiveautomation.com/t/tried-to-access-key-from-dictionary-and-facing-an-issue/60246/4 "2022-06-01T12:36:55Z")

</div>

You should configure rosters instead, and use those in the notification block.

But if you don’t want to do that, then I’d suggest something like this, supposing you only want the email addresses:

```python
ds = system.db.runNamedQuery(“FetchUsers”)
pds = system.dataset.toPyDataSet(ds)
email_addresses = [row[contact_value] for row in pds if row['contact_type'] == "email"]

```

No need to bring json into this.
