Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
589 B
5
Indexable
Never
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))
Leave a Comment