Python Using Marshmallow To Load a JSON Object into a collection of Python Class

4k Views Asked by At

I am attempting to load a JSON Object (stored in a .JSON file) into a collection of Python classes using the Marshmallow module. I have created both my class and my class schema. However, when I attempt to use the Schema to load the data I get the following error:

{0: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 1: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 2: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 3: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 4: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 5: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 6: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 7: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 8: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 9: {'GroupId': ['Unknown field.'],
     'Id': ['Unknown field.'],
     'Name': ['Unknown field.']},
 10: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 11: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 12: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 13: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 14: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 15: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 16: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 17: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 18: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']},
 19: {'GroupId': ['Unknown field.'],
      'Id': ['Unknown field.'],
      'Name': ['Unknown field.']}}

To simplify the answer of the question, I have packed the solution into the following files:

stackoverflow.py:

    from marshmallow import Schema, fields, post_load, ValidationError
    import json
    from pprint import pprint
    
    
    class Person:
        def __init__(self, id, name, groupId):
            self.id = id
            self.name = name
            self.groupId = groupId
    
        def __repr__(self):
            return f"{self.id}, {self.name}, {self.groupId}"
    
    
    class PersonSchema(Schema):
        id = fields.Integer()
        name = fields.String()
        groupId = fields.Integer()
    
        @post_load
        def create_person(self, data, **kwargs):
            return Person(**data)
    
    
    with open("data.json") as json_file:
     
        data = json.load(json_file)
        try:
            PersonSchema(many=True).load(data)
        except ValidationError as err:
            pprint(err.messages)

data.json:

    [
  {
    "Id": 1,
    "Name": "Alex",
    "GroupId": 1
  },
  {
    "Id": 2,
    "Name": "Bob",
    "GroupId": 1
  },
  {
    "Id": 3,
    "Name": "Charlie",
    "GroupId": 1
  },
  {
    "Id": 4,
    "Name": "Daniel",
    "GroupId": 2
  },
  {
    "Id": 5,
    "Name": "Emma",
    "GroupId": 2
  },
  {
    "Id": 6,
    "Name": "Fran",
    "GroupId": 3
  },
  {
    "Id": 7,
    "Name": "George",
    "GroupId": 3
  },
  {
    "Id": 8,
    "Name": "Harry",
    "GroupId": 2
  },
  {
    "Id": 9,
    "Name": "Ian",
    "GroupId": 2
  },
  {
    "Id": 11,
    "Name": "James",
    "GroupId": 2
  },
  {
    "Id": 12,
    "Name": "Kevin",
    "GroupId": 2
  },
  {
    "Id": 13,
    "Name": "Louis",
    "GroupId": 1
  },
  {
    "Id": 14,
    "Name": "Martin",
    "GroupId": 1
  },
  {
    "Id": 15,
    "Name": "Norman",
    "GroupId": 1
  },
  {
    "Id": 16,
    "Name": "Oscar",
    "GroupId": 1
  },
  {
    "Id": 17,
    "Name": "Paul",
    "GroupId": 1
  },
  {
    "Id": 18,
    "Name": "Quintin",
    "GroupId": 1
  },
  {
    "Id": 19,
    "Name": "Roger",
    "GroupId": 2
  },
  {
    "Id": 20,
    "Name": "Steve",
    "GroupId": 2
  },
  {
    "Id": 21,
    "Name": "Terry",
    "GroupId": 2
  }
]
1

There are 1 best solutions below

2
On

I this line :

PersonSchema(many=True).loads(data)

data is a python object, not a string. I suppose you have to use load instead of loads.