squib1996
squib1996

Reputation: 67

List in Dictionary

I can't seem to easily access a list value from within a dictionary response from an API.

data = {
'room_id': room,
'how_many': 1
 }

 response_url = 'https://api.clickmeeting.com/v1/conferences/'+ str(room) +'/tokens'



 response1 = requests.post(response_url, headers=headers, data=data).  

 response1.raise_for_status()
 # access JSOn content
 jsonResponse = response1.json()

 print(jsonResponse)

the response is: {'access_tokens': [{'token': 'C63GJS', 'sent_to_email': None, 'first_use_date': None}]}

I'm looking to assign the token value to a variable.

Any ideas?

Upvotes: 0

Views: 61

Answers (1)

gallen
gallen

Reputation: 1292

If the list in the access_tokens is always of length 1, you can do something like this:

token = json_response["access_token"][0]["token"]

If there's a potential for more than one item in access_tokens, then something similar:

tokens = []
access_tokens = json_response["access_token"]
tokens = [at["token"] if "token" in at for at in access_tokens]

Upvotes: 1

Related Questions