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": "EP@google.com",
"fullname": "null"
},
{
"username": "pE@google.com",
"fullname": "null"
}
]
}
role = json.loads(role)
for item in role['principals']:
p = item['username']
print(p)
It is printing as below:
EP@google.com
pE@google.com
Expected output:
['EP@google.com','pE@google.com']
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]