How can I return the value of a key from data that is in json format?

Jesus Javier Alvarez July 20, 2017

I recieved a server response (r = request.get(url,auth) with data that I searched using 'api/2/search?jql=' with certain paramaters I need. However, how do I get or print the issueId number (only) of the tickets in the list of the search? 

For example: 

The data would look like:

{u'issues': [{u'key': u'x-123', u'fields': {u'customfield_123': None}......}

And, I would like to get the value/string 'x-123' (only) from 'key'.

I have done the following, but to no avail:

r.json()['issues'] #returns all values for issues

r.json()['issues':'key'] # returns TypeError: unhashable type.

 

Thanks in advance for those who help.

2 answers

0 votes
creig malta November 15, 2021

"r.json()['issues':'key'] # returns TypeError: unhashable type."

 

The problem is that you can't use a list as the key in a dict, since dict keys need to be immutable. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed. The standard way to solve this issue is to cast a list to a tuple . TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. The standard way to solve this issue is to cast a list to tuple.

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as key.

0 votes
Jesus Javier Alvarez July 21, 2017

Nevermind. I figured it out.

It is a dictionary nested in a list that is nested in a dictionary. Therefore, the correct call would look something like:                

data[‘issues'][0][‘key']

Thanks anyway.

Suggest an answer

Log in or Sign up to answer