-
-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help to deserialize complex JSON #873
Comments
What version of marshmallow are you using? Can you edit your example so that the data and the schema match? |
The example is correct! |
I tried this way but didn't work: phones = fields.Nested(Phone, load_from='phones.list', many=True) |
Loading from a nested field name is not currently supported. #611 is a feature request for that syntax. |
But there is another way to do this without using |
Create a schema for the intermediate container, then "pluck" the list value out of it into the parent schema. class Phone(OrderedSchema):
id = fields.Int()
email = fields.Str()
class Phones(Schema):
list = fields.Nested(Phone, many=True)
class Contact(Schema):
key = fields.Str()
phones = fields.Nested(Phones, only='list') #823 should make this a little more intuitive in v3 with a dedicated |
I try this way: class Item(OrderedSchema):
id = fields.Int()
class Phone(OrderedSchema):
items = fields.Nested(Item, many=True)
class Contact(OrderedSchema):
key = fields.Str()
phones = fields.Nested(Phone, only='items') But not works: [
{
"key": "1042F608-84C0-4F11-A685-61D06394EF0D",
"phones": {
"items": [
{
"id": 1
}
]
}
}
] The expected result is: [
{
"key": "1042F608-84C0-4F11-A685-61D06394EF0D",
"phones": [
{
"id": 1
}
]
}
] |
Can u help me @deckar01? I try update to 3.0.0b12 (latest beta version) to use With phones = fields.Pluck(Phone, 'items', many=True) |
|
@lafrech but there is another way to do this without using |
See #873 (comment). |
data = {
"emails": {
"items": [
{
"id": '1',
"email": "[email protected]"
},
{
"id": '2',
"email": "[email protected]"
}
]
}
}
class Email(marshmallow.Schema):
id = marshmallow.fields.Int()
email = marshmallow.fields.Email()
class Emails(marshmallow.Schema):
emails = marshmallow.fields.Nested(Email, many=True)
@marshmallow.pre_load
def load_emails(self, data):
data['emails'] = data['emails']['items']
return data
print(Emails().load(data))
# {'emails': [{'id': 1, 'email': '[email protected]'}, {'id': 2, 'email': '[email protected]'}]} |
Please reopen if still stuck. BTW, |
I need to deserialize this JSON:
Into this object using Marshmallow:
How can I do it?
I tried this way, which I found more intuitive, but it did not work:
The text was updated successfully, but these errors were encountered: