-1

I have the Employee classs which has name age and the Addresses list, which contains the list of addresses for the employee.

My Code design are as follows

class Employee:
    Name=''
    Age =''
    Addresses =[]

class Address:
   State =''
   District =''
   City=''
adress1 =Address();
adress1.State='West bengal'
adress1.District ='Jalpaiguri'
adress1.City= 'Kolkata'

adress2 =Address()
adress2.State='West Bengal'
adress2.District ='Darjeeling'
adress2.City= 'Siliguri'
employee =Employee()
employee.name ='Gunjan'
employee.age =22
employee.Addresses.append(adress1)
employee.Addresses.append(adress2)

I want to convert the employee to json representation. I tried

json.dumps(employee.__dict__).

But this is giving me only the name and age property of Employee but not the list of addresses it contains.

It would be great help if someone would guide me to get the perfect json representation something like-

{"name":"Gunjan","Age":"22","Addresses" :[{"State":"West Bengal","City":"Siliguri","District":"Darjeeling"},{"State":"West Bengal","City":"Kolkata","District":"New Jalpaiguri"}]}

Thank you

Gunjan chhetri
  • 127
  • 1
  • 1
  • 6

2 Answers2

1

In fact, you are trying to create a dictionary which can be converted to JSON, you are not directly creating JSON representation (Python allows both techniques).

To create dictionary, try this:

class Employee(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.addresses = []

    @property
    def __dict__(self):
        return {
            "name": self.name,
            "age": self.age,
            "addresses": [addr.__dict__ for addr in self.addresses]
        }


class Address(object):
    def __init__(self, state, district, city):
        self.state = state
        self.district = district
        self.city = city

employee = Employee("Gunjan", 22)
employee.addresses.append(Address("West Bengal", "Jalpaiguri", "Kolkata"))
employee.addresses.append(Address("West Bengal", "Darjeeling", "Siliguri"))

print employee.__dict__

It shall print:

{'age': 22, 'name': 'Gunjan', 'addresses': [{'city': 'Kolkata', 'state': 'West Bengal', 'district': 'Jalpaiguri'}, {'city': 'Siliguri', 'state': 'West Bengal', 'district': 'Darjeeling'}]}
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
0

Few things, the convention in python for class members is lowercase.

Another problem is that you called emplyee.name and your member is Name.

If you want to use data members in your class you should write self.name for example.

As you can see i implemented the dump function, if a class has list of objects it's kind of problem and i don't know any straightforward solution.

I wrote for you code that does your intention.

import json
class Employee:
    def __init__(self):
        self.name=''
        self.age =''
        self.addresses =[]

    def dump(self):
        return {"Name": self.name,
                "Age": self.age,
                "Addresses": [addr.__dict__ for addr in self.addresses]}

class Address:
    def __init__(self):
       self.state =''
       self.district =''
       self.city=''

adress1 =Address()
adress1.state='West bengal'
adress1.district ='Jalpaiguri'
adress1.city= 'Kolkata'

adress2 =Address()
adress2.state='West Bengal'
adress2.district ='Darjeeling'
adress2.city= 'Siliguri'

employee =Employee()
employee.name ='Gunjan'
employee.age =22
employee.addresses.append(adress1)
employee.addresses.append(adress2)

print json.dumps(employee.dump())

Result:

{"Age": 22, "Name": "Gunjan", "Addresses": [{"City": "", "State": "", "District": ""}, {"City": "Siliguri", "State": "West Bengal", "District": "Darjeeling"}]}
omri_saadon
  • 10,193
  • 7
  • 33
  • 58