Skip to content

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