flatten
flatten copied to clipboard
fix unflatten error when missing parent key
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.
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)