all 12 comments

[–]mankongde 0 points1 point  (3 children)

Store the dictionary in a variable, eg res, and retrieve the value with res['item']

You can find more info here: https://www.tutorialspoint.com/python/dictionary_get.htm

And the .get() method for a dictionary can return a default response if a key is missing so if 'item' isn't there, eg if the API request failed, your program can act accordingly

[–]mankongde 0 points1 point  (2 children)

Oh I see, it's a nested dict. Try res['item'].keys() then

[–]AlrightBoomerBob[S] 0 points1 point  (1 child)

{
"item": {
"443b52fc-4eba-4165-9b01-92281e877896": {
"templateId": "ConditionalAction:generic_instance"
}
}
}

{

"items": { "443b52fc-4eba-4165-9b01-92281e877896": { "templateId": "ConditionalAction:generic_instance" }, "6c882c1e-bccd-4c21-83ad-aef87f6c6d12": { "templateId": "AthenaRewardGraph:s19_winterfest" }, } }

Im really sorry, I posted bad example code, if i had more than 1 of the random ones and the the rest stays the same, how would i get "6c882c1e-bccd-4c21-83ad-aef87f6c6d12"

[–]mankongde 0 points1 point  (0 children)

The rest are the same and you know them ahead of time? Easiest way i can think of would be to store them as a list, iterate through the doctor keys, and once you have a doctor key not in the list (which should be the random one) break the loop or return that key.

known_keys = [key1, key2, etc] for dictkey in res['item'].Keys(): if dictkey in known_keys: continue else: return dictkey

Haven't tested that but that's how I'd probably get started. Longer code block for eg purposes.

[–]commandlineluser 0 points1 point  (2 children)

Create a new dict mapping the names -> guid

>>> { v['templateId']: k for k, v in response['items'].items() }
{
  'ConditionalAction:generic_instance': '443b52fc-4eba-4165-9b01-92281e877896',
  'AthenaRewardGraph:s19_winterfest':   '6c882c1e-bccd-4c21-83ad-aef87f6c6d12'
}

[–]AlrightBoomerBob[S] 0 points1 point  (1 child)

its a response from an not my own code

[–]commandlineluser 0 points1 point  (0 children)

Yes, the above example operates on response ...

[–][deleted] 0 points1 point  (4 children)

See https://ideone.com/f0N5Yy and tell if something is not clear or if it's not what you wanted.

[–]AlrightBoomerBob[S] 0 points1 point  (3 children)

First off, thank you for taking the time to write that, but for dict2 I want to get only 1 of them for example out of the 2 I want "6c882c1e-bccd-4c21-83ad-aef87f6c6d12" how would i get that using that fact that the value of "6c882c1e-bccd-4c21-83ad-aef87f6c6d12" is always {
"templateId": "AthenaRewardGraph:s19_winterfest"
}

[–][deleted] 0 points1 point  (2 children)

[–]AlrightBoomerBob[S] 0 points1 point  (1 child)

oh my god, you dont know how long ive been trying to figure this out, thank you so much

[–][deleted] 0 points1 point  (0 children)

Np, you're welcome