-2

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": "reid.warren@undefined.name",
    "phone": "+1 (983) 443-3504",
    "address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
  },
  {
    "profilename": "QTest123"
    "email": "amelia.wilkinson@undefined.us",
    "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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Yves1234
  • 35
  • 1
  • 7

3 Answers3

2

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": "reid.warren@undefined.name",
        "phone": "+1 (983) 443-3504",
        "address": "359 Rapelye Street, Holtville, Marshall Islands, 9692",
    },
    {
        "profilename": "QTest123",
        "email": "amelia.wilkinson@undefined.us",
        "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: reid.warren@undefined.name

To load the profiles from a file:

import json

with open("profiles.json", "r") as f:
    profiles = json.loads(f.read())
ptts
  • 1,848
  • 6
  • 14
  • `next`'s second argument defaults to `None` if you don't provide it, isn't it? – Asocia May 04 '21 at 09:43
  • 1
    No, without the second argument, a `StopIteration` Exception is raised if no match is found. From the documentation: *"If default is given and the iterator is exhausted, it is returned instead of raising StopIteration."* – ptts May 04 '21 at 09:44
  • Yeah basivally thats what i wnated to know, thanks! Just one more question, is it also possible with opening the JSON file? like having all the informations saved into a seperate JSON file, without having the informations in the .py file? So that it reads the information from a seperate JSON file – Yves1234 May 04 '21 at 10:05
  • I've added example code on how to load json from a separate file. This code assumes that you have the data saved in a file named `profiles.json` in the current directory. – ptts May 04 '21 at 10:09
  • Thanks bro!! It worked, really appreciate it, have a great day!!! – Yves1234 May 04 '21 at 11:08
  • @Yves1234 Best way to say *"thanks"* is accepting the answer. Since you have more than 15 reputation, you can also vote up! – Asocia May 05 '21 at 00:55
1
import json

json = [
  {
    "profilename": "Test123",
    "email": "reid.warren@undefined.name",
    "phone": "+1 (983) 443-3504",
    "address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
  },
  {
    "profilename": "QTest123",
    "email": "amelia.wilkinson@undefined.us",
    "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'])
>>>reid.warren@undefined.name

dtc348
  • 309
  • 6
  • 19
0
  1. Deserialize the data into a python object (list of dictionaries in this case):
import json

json_str = '''[
  {
    "profilename": "Test123",
    "email": "reid.warren@undefined.name",
    "phone": "+1 (983) 443-3504",
    "address": "359 Rapelye Street, Holtville, Marshall Islands, 9692"
  },
  {
    "profilename": "QTest123",
    "email": "amelia.wilkinson@undefined.us",
    "phone": "+1 (831) 563-3240",
    "address": "525 Allen Avenue, Iola, Kentucky, 894"
  }
]'''

list_of_dicts = json.loads(json_str)
  1. 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.

sur0k
  • 334
  • 1
  • 6
  • 1
    You can remove `try .. except ...` statement if you are going to re-raise the exception without doing anything with it. – Asocia May 04 '21 at 09:44