0

I've finally started learning Python and finished my first Udemy course, and started my project. I tried searching the forum but couldn't really find an answer to this directly.

I have an API request to an employee database in JSON, which simplified looks like this:

"Employees": {
"Arny Arnoldsen": {
    "Employee_ID" : "0001",
    "Title" : "Accounts Manager",
    "Address 1" : "Cheery road 3",
    "Address 2" : "Georgia"
    },

"Burt Burtsen": {
    "Employee_ID" : "0002",
    "Title" : "Sales Manager",
    "Address 1" : "Destiny street 23",
    "Address 2" : "Las Vegas"
    },
"Carl Carlsen": {
    "Employee_ID" : "0003",
    "Title" : "Operational Manager",
    "Address 1" : "Sunshine road 42",
    "Address 2" : "Miami"
    }
}

To get my list of employes I just use

for employee in employees: 
  print(employee)

My second problem arises when I want to search for an employee using the Employee_ID. Because of GDPR etc etc I don't get any names, but might have f.eks. "Employee_ID" 0002 and I need to return Burt Burtsen.

I can look 'down' into dictionaries and lists, but can't seem to figure out how to do the opposite. I hope someone in here can help :)

Jongware
  • 22,200
  • 8
  • 54
  • 100
Kemad
  • 3
  • 3
  • And what's the first problem? – Pedro Lobito Apr 24 '20 at 15:59
  • I am going on a limb as assume that the `employees` is a variable that holds the json that you posted. To cycle through the list you would have to get the value of the `Empoyees` key. `for employee in employees['Employees']: print(employee)`, this will print the name of each employee. If you are doing a within this same loop you would check if the id matches and then return the `employee` variable. That would be the name you the employee that matches the id you supplied. `employees['Employees'][employee]['Employee_ID'] == `, within the loop. Python is a fun lang good luck. – Abass Sesay Apr 24 '20 at 16:13
  • The first issue was just listing the employees - which I could do myself. Probably poorly worded. Abass Sesay your answer is the same as ex4 - it works excellently - thanks! I'm having much fun with Python :) – Kemad Apr 24 '20 at 16:25

2 Answers2

0

Like this

data = {"Employees": {
"Arny Arnoldsen": {
    "Employee_ID" : "0001",
    "Title" : "Accounts Manager",
    "Address 1" : "Cheery road 3",
    "Address 2" : "Georgia"
    },

"Burt Burtsen": {
    "Employee_ID" : "0002",
    "Title" : "Sales Manager",
    "Address 1" : "Destiny street 23",
    "Address 2" : "Las Vegas"
    },
"Carl Carlsen": {
    "Employee_ID" : "0003",
    "Title" : "Operational Manager",
    "Address 1" : "Sunshine road 42",
    "Address 2" : "Miami"
    }
   }
}

for emp in data["Employees"]:
    if data["Employees"][emp]["Employee_ID"] == "0002": 
        print(emp)

When running foreach loop for dict you get every key as your iteration variable. And employee name is key in your data structure.

ex4
  • 2,289
  • 1
  • 14
  • 21
  • This worked like a charm. Thanks :) Have worked a little with the for x in y loop - didn't think of using it like this to do a test. – Kemad Apr 24 '20 at 16:23
0

A simple way would be:

d = {
    "Employees": {
        "Arny Arnoldsen": {
            "Employee_ID" : "0001",
            "Title" : "Accounts Manager",
            "Address 1" : "Cheery road 3",
            "Address 2" : "Georgia"
        },

        "Burt Burtsen": {
            "Employee_ID" : "0002",
            "Title" : "Sales Manager",
            "Address 1" : "Destiny street 23",
            "Address 2" : "Las Vegas"
        },
        "Carl Carlsen": {
            "Employee_ID" : "0003",
            "Title" : "Operational Manager",
            "Address 1" : "Sunshine road 42",
            "Address 2" : "Miami"
        }
    }
}

def find_emp(d, query):
    for k,v in d['Employees'].items():
        if v.get("Employee_ID") == query:
            return k

emp_name = find_emp(d, "0003")
# Carl Carlsen

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I'll be honest and say I don't understand this version as much as the above ... yet ..., since I haven't tried too much with functions like this. But it works like a charm - and functions more like a function, which I'll definitely use down the line! A little self study on your code and this will be perfect. Thanks :) – Kemad Apr 24 '20 at 16:33