How to search for Keywords in JSON
Question:
i am using python json and i want to let my Python code search for specefic Keywords in a JSON-File.
Basically it should search for the "profilename" and then go one line down and print the email of the profile out.
[
{
"profilename": "Test123"
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123"
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]
Like the code should search for profilename "Test123" and print out the email of that, like going one line down and print the email out.
I tried many things but i didnt even come one step closer, so sharing my current code would help 0 :/
Thanks.
Answers:
If I understand you correctly, you are trying to find a profile by the field profilename
and return the user’s email
.
profiles = [
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692",
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894",
},
]
def get_profile_email(profilename):
profile = next(
(item for item in profiles if item["profilename"] == profilename), None
)
if profile:
return profile["email"]
return None
print(get_profile_email("Test123"))
Output:
[email protected]
To load the profiles from a file:
import json
with open("profiles.json", "r") as f:
profiles = json.loads(f.read())
- Deserialize the data into a python object (list of dictionaries in this case):
import json
json_str = '''[
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]'''
list_of_dicts = json.loads(json_str)
- Then find and print out your entry:
profile_entry = next(el for el in list_of_dicts if el['profilename'] == 'Test123')
print(profile_entry['email'])
StopIteration
occurs when you have not profilename == Test123
in your data. More about the list of dictionaries search here.
import json
json = [
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]
profile_name = "Test123"
data = [x for x in json if x['profilename'] in profile_name]
print(data[0]['email'])
>>>[email protected]
i am using python json and i want to let my Python code search for specefic Keywords in a JSON-File.
Basically it should search for the "profilename" and then go one line down and print the email of the profile out.
[
{
"profilename": "Test123"
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123"
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]
Like the code should search for profilename "Test123" and print out the email of that, like going one line down and print the email out.
I tried many things but i didnt even come one step closer, so sharing my current code would help 0 :/
Thanks.
If I understand you correctly, you are trying to find a profile by the field profilename
and return the user’s email
.
profiles = [
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692",
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894",
},
]
def get_profile_email(profilename):
profile = next(
(item for item in profiles if item["profilename"] == profilename), None
)
if profile:
return profile["email"]
return None
print(get_profile_email("Test123"))
Output:
[email protected]
To load the profiles from a file:
import json
with open("profiles.json", "r") as f:
profiles = json.loads(f.read())
- Deserialize the data into a python object (list of dictionaries in this case):
import json
json_str = '''[
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]'''
list_of_dicts = json.loads(json_str)
- Then find and print out your entry:
profile_entry = next(el for el in list_of_dicts if el['profilename'] == 'Test123')
print(profile_entry['email'])
StopIteration
occurs when you have not profilename == Test123
in your data. More about the list of dictionaries search here.
import json
json = [
{
"profilename": "Test123",
"email": "[email protected]",
"phone": "+1 (983) 443-3504",
"address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
},
{
"profilename": "QTest123",
"email": "[email protected]",
"phone": "+1 (831) 563-3240",
"address": "525 Allen Avenue, Iola, Kentucky, 894"
}
]
profile_name = "Test123"
data = [x for x in json if x['profilename'] in profile_name]
print(data[0]['email'])
>>>[email protected]