Python Forum
Deserialize Complex Json to object using Marshmallow
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deserialize Complex Json to object using Marshmallow
#1
Hello, I am trying to deserialize json into an object that has nested objects within it.

I am able to deserialize the json into the root level object and the rest of the objects do have values, but I can't go further into the structure.

For example

class CustomerDTO(BaseDTO)
    first_name: str = fields.Str(required=True)
    last_name: str = fields.Str(required=True)
    address: AddressDTO = fields.Nested(AddressDTOSchema, required=True)

class CustomerDTOSchema(Schema, CustomerDTO)
    pass

class AddressDTO(BaseDTO)
    street_name_one: str = fields.Str(required=True)
    street_name_two: str = fields.Str(required=False)
    city: str = fields.Str(required=True)
    state: str = fields.Str(required=True)
    zip_code: str = fields.Str(required=True)

class AddressDTOSchema(Schema, AddressDTO)
    pass
If I run something like

customer_data = request.get_json()
customer_dto_schema = CustomerSchema().load(customer_data)
customer_dto = CustomerDTO(**customer_dto_schema)

print(customer_dto) #this works fine
print(customer_dto.first_name) # this works fine
print(customer_dto.address) # this works fine
print(custome_dto.address.street_name_one) # I get an error here -> 'dict' object has no attribute 'street_name_one'
How can I have this cascade down into the nested objects?
Reply
#2
Then you have to inspect what request.get_json() gives as a result.
Reply
#3
(Dec-09-2021, 06:23 PM)ibreeden Wrote: Then you have to inspect what request.get_json() gives as a result.

The request_json gives me this

{
  'first_name': 'John',
  'last_name': 'Doe',
  'address': {
    'street_name_one': '123 Main Street',
    'street_name_two': 'Apt 123',
    'city': 'TestCity',
    'state': 'TX',
    'zip_code': '12345'
   }
}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding and deleting json object GrahamL 1 4,854 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  Object of type set is not JSON serializable enigma619 5 12,273 Dec-19-2019, 03:30 PM
Last Post: enigma619
  Object of type Scoreboard is not JSON serializable lsepolis123 9 9,466 Aug-13-2019, 11:22 AM
Last Post: lsepolis123
  Object type of Node to json bhojendra 4 4,546 Apr-28-2019, 01:36 PM
Last Post: bhojendra
  Reading complex JSON dictionaries Gromis 5 3,855 Jul-08-2018, 10:26 PM
Last Post: Gromis
  Object of type 'Status' is not JSON serializable ndakena 1 4,446 May-28-2018, 06:35 AM
Last Post: buran
  Object madness - JSON Notation confusion execsys 4 3,699 May-03-2018, 08:56 AM
Last Post: buran
  serialize/deserialize data from/to json bb8 1 2,910 Mar-09-2018, 08:11 PM
Last Post: Gribouillis
  want to know the kind of object whether its a python or json object johnkennykumar 5 62,528 Jan-21-2017, 08:47 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020