I want to parse the vaule of user from json response and send email to those id`s using python2.7
I can send it to one user, but not sure how to embed multiple users
role = {
"permissions": [],
"principals": [
{
"username": "[email protected]",
"fullname": "null"
},
{
"username": "[email protected]",
"fullname": "null"
}
]
}
role = json.loads(role)
for item in role['principals']:
p = item['username']
print(p)
It is printing as below:
[email protected]
[email protected]
Expected output:
['[email protected]','[email protected]']
You can use list comprehension:
p = [item['username'] for item in role['principals']]
print(p)
Safe variant (won't raise error if key doesn't exist):
result = []
for item in role.get('principals', []):
p = item.get('username', None)
if p:
result.append(p)
print(result)
Safe list comprehension:
result = [item['username'] for item in role.get('principals', []) if 'username' in item]