Skip to content

fix unflatten error when missing parent key #77

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

felixlu
Copy link

@felixlu felixlu commented Aug 24, 2021

Summary

When unflatten with the following test data, an exception was raised: 'NoneType' object has no attribute 'setdefault'

from flatten_json import unflatten_list

test_dic = {
  'header': None, 
  'header.errcode': 0, 
  'header.errmsg': '', 
  'term_group': None, 
  'term_group.contents': None, 
  'term_group.group_id': 'USER_TERM_GROUP_NONE', 
  'term_group.login_config': None, 
  'term_group.login_config.check_boxes': None, 
  'term_group.terms': None, 
  'term_group.terms.0.contents': None  # missing parent key 'term_group.terms.0'
}

unflatten_list(test_dic, '.')

Bug Fixes/New Features

  • fixed the above error.

How to Verify

Use the above test data.

```
from flatten_json import unflatten_list

test_dic = {
'header': None, 
'header.errcode': 0, 
'header.errmsg': '', 
'term_group': None, 
'term_group.contents': None, 
'term_group.group_id': 'USER_TERM_GROUP_NONE', 
'term_group.login_config': None, 
'term_group.login_config.check_boxes': None, 
'term_group.terms': None, 
'term_group.terms.0.contents': None  # missing parent key 'term_group.terms.0'
}

unflatten_list(test_dic, '.')
```
…g-parent-key

fix unflatten error when missing parent key
@amirziai
Copy link
Owner

thanks for contributing @felixlu .

do you mind adding a test case that shows the expected behavior? e.g.

    def test_unflatten_missing_parent(self):
        dic = {
            'term_group.terms': None,
            'term_group.terms.0.contents': None,
        }
        expected = {
            'term_group': {'terms': [{'contents': None}]}
        }
        actual = unflatten_list(dic, '.')
        self.assertEqual(expected, actual)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants