soBusted
soBusted

Reputation: 305

Filter item in dictionary inside a list

I searched for this issue and found many similar questions, but.. none that I could either understood the answer or could transform to my use case.

I know how to filter a response like this {"type":"hello"}, response['type']
>>> hello
But when I got this [{"mail_address":"[email protected]"}] I was a bit lost.. I've tried a couple of ways, and the last solution I settled on was a bit ugly...

res= [{"mail_address":"[email protected]"}]  <-- using request method>
body = (res.text)
a = body[18:100]    <--- 100, because I don't really know the length of the mail>
b = ''.join(a.split())
foo = b[:-3]
print(foo)
>>> [email protected]

I wonder , There must be a better \ cleaner way ?

Upvotes: 0

Views: 46

Answers (1)

user5386938
user5386938

Reputation:

If you indeed are getting what looks to be a JSON string as a response from some web call then perhaps the following can help.

import json

s = '[{"mail_address":"[email protected]"}]'  # from resp.text

o = json.loads(s)

print(o[0]['mail_address'])


Upvotes: 3

Related Questions