Untitled

 avatar
unknown
python
a year ago
589 B
11
Indexable
import json

with open('data.json') as f:
    data = json.load(f)

attributes = set()
for emp_data in data['employees']:
    attributes.update(emp_data.keys())

class Employee:
    def __init__(self, **kwargs):
        for attr in attributes:
            setattr(self, attr, kwargs.get(attr))

employees = []
for emp_data in data['employees']:
    emp = Employee(**emp_data)
    employees.append(emp)

print("case 1:")
for emp in employees:
    print((emp.name, emp.salary))

print("case 2:")
for emp in [emp for emp in employees if emp.status == "True"]:
    print((emp.name, emp.salary))
Editor is loading...
Leave a Comment