flatten icon indicating copy to clipboard operation
flatten copied to clipboard

fix unflatten error when missing parent key

Open felixlu opened this issue 3 years ago • 1 comments

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.

felixlu avatar Aug 24 '21 03:08 felixlu

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)

amirziai avatar Aug 24 '21 04:08 amirziai