question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Merge nested data with the same 'data_key'

See original GitHub issue
from marshmallow import Schema, fields, EXCLUDE
from pprint import pprint

data = {
    'username' : "John",
    'address': {
        'phone': '88000111000',
        'street': 'Kolobko Str',
        'home': 15,
    },
}

class Address:
    def __init__(self, *args, **kwargs):
        self.street = kwargs.get('street')
        self.home = kwargs.get('home')

class Contacts:
    def __init__(self, *args, **kwargs):
        self.phone = kwargs.get('phone')

class User:
    def __init__(self, *args, **kwargs):
        self.name = kwargs.get('name')
        self.address = kwargs.get('address')
        self.contacts = kwargs.get('contacts')


class AddressSchema(Schema):
    street = fields.String()
    home = fields.Integer()

class ContactsSchema(Schema):
    phone = fields.String()

class UserSchema(Schema):

    name = fields.String(data_key='username')
    address = fields.Nested(AddressSchema, only=['home', 'street'], unknown=EXCLUDE)
    contacts = fields.Nested(ContactsSchema, only=['phone'], data_key='address', unknown=EXCLUDE)

schema = UserSchema()
user = schema.load(data)    
res = schema.dump(user)
pprint(res, indent=2)

code above show different results:

➜ python test_marshmallow/test_nested_fields.py
{'address': {'home': 15, 'street': 'Kolobko Str'}, 'username': 'John'}

➜ python test_marshmallow/test_nested_fields.py
{'address': {'phone': '88000111000'}, 'username': 'John'}

while expected:

{
    'username': 'John',
    'address': {
        'home': 15, 
        'street': 'Kolobko Str',
        'phone': '88000111000'
    }
}

marshmallow==3.0.0b16

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
edelooffcommented, Sep 28, 2018

Having this raise an exception seems like it’s the correct solution, given that this is essentially a configuration error. Ideally this would be raised during creation of the schema, helping with visibility.

Quietly (and non-deterministically) doing stuff to the data going through the schema is likely to result in serious and hard to understand bugs.

1reaction
lafrechcommented, Oct 11, 2018

I just sent a PR to raise a ValueError in case of duplicate: https://github.com/marshmallow-code/marshmallow/pull/992.

@ehles, @edelooff, please have a look and comment if you have the time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Merge Object keys and nested array data - Stack Overflow
Assuming you know that all of the properties have the same length, this would probably do the trick: let obj = {}; //Put...
Read more >
CDC using Merge - Databricks
Change data capture (CDC) is a type of workload where you want to merge the reported row changes from another database into your...
Read more >
Reshaping Your Data with tidyr
This leads to difficult-to-read nested functions and/or choppy code. ... Function: gather(data, key, value, ..., na.rm = FALSE, convert = FALSE) Same as: ......
Read more >
Report Tables - Ignition User Manual 8.1
If the table needs to display values from multiple data sources, then a Nested Query should be used to collect all the values,...
Read more >
array_replace_recursive - Manual - PHP
array_replace_recursive() replaces the values of array with the same values from all the ... array_merge_recursive() - Merge one or more arrays recursively.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found