Reputation: 13
I do have the following sample JSON file
[
{
"name": "A"
"email": "[email protected]",
"admin": false,
"groupAdmin": false,
"profileUpdatable": true
},
{"name": "B"
"email": "[email protected]",
"admin": false,
"groupAdmin": false,
"profileUpdatable": true
},
{"name": "C"
"email": "[email protected]",
"admin": false,
"groupAdmin": false,
"profileUpdatable": true
},
{"name": "A"
"admin": false,
"groupAdmin": false,
"profileUpdatable": true
}
]
I want to do the following using python3:
Thanks in advance
Upvotes: 1
Views: 414
Reputation: 606
You don´t have to remove the blocks. Iterate over the persons and take the email key if present.
By the way, you have an error in your file. After the value for name, a comma is missing in each object.
data = [
{
"name": "A",
"email": "[email protected]",
"admin": False,
"groupAdmin": False,
"profileUpdatable": True
},
{"name": "B",
"email": "[email protected]",
"admin": False,
"groupAdmin": False,
"profileUpdatable": True
},
{"name": "C",
"email": "[email protected]",
"admin": False,
"groupAdmin": False,
"profileUpdatable": True
},
{"name": "A",
"admin": False,
"groupAdmin": False,
"profileUpdatable": True
}
]
def get_emails(data: list):
e_mails = []
for person in data:
if "email" in person:
e_mails.append(person["email"])
return e_mails
if __name__ == "__main__":
print(get_emails(data=data))
Upvotes: 2