flatten icon indicating copy to clipboard operation
flatten copied to clipboard

Unable to use flatten + unflatten when original dict key names contain seperator char

Open rorosentrater opened this issue 5 years ago • 1 comments

Hi, I like this library but it has 1 flaw preventing me from using it. I am unable to flatten and unflatten dictionaries that include the separating character. I only noticed this because I use underscores in my key names a lot and that is the default separating char for this library. Example:

from flatten_json import flatten, unflatten_list
import pprint

starter_dict = {
    'normal': 'kskdaskad',
    'nested': {'dict': 'asdasd'},
    'array': [1, 2, 3],
    'deeparray': [
        'string',
        {'key': 'val'},
        ['yet', 'another', 'list', ['moar']]
    ],
    'MY_KEY_WHICH_INCLUDES_UNDERSCORES': 'a single value'
}
print('-------------- original dict --------------')
pprint.pprint(starter_dict)
flat = flatten(starter_dict)
print('-------------- flat packed --------------')
pprint.pprint(flat)
unflat = unflatten_list(flat)
print('-------------- unpacked --------------')
pprint.pprint(unflat)

Output:

-------------- original dict --------------
{'MY_KEY_WHICH_INCLUDES_UNDERSCORES': 'a single value',
 'array': [1, 2, 3],
 'deeparray': ['string', {'key': 'val'}, ['yet', 'another', 'list', ['moar']]],
 'nested': {'dict': 'asdasd'},
 'normal': 'kskdaskad'}
-------------- flat packed --------------
{'MY_KEY_WHICH_INCLUDES_UNDERSCORES': 'a single value',
 'array_0': 1,
 'array_1': 2,
 'array_2': 3,
 'deeparray_0': 'string',
 'deeparray_1_key': 'val',
 'deeparray_2_0': 'yet',
 'deeparray_2_1': 'another',
 'deeparray_2_2': 'list',
 'deeparray_2_3_0': 'moar',
 'nested_dict': 'asdasd',
 'normal': 'kskdaskad'}
-------------- unpacked --------------
{'MY': {'KEY': {'WHICH': {'INCLUDES': {'UNDERSCORES': 'a single value'}}}},
 'array': [1, 2, 3],
 'deeparray': ['string', {'key': 'val'}, ['yet', 'another', 'list', ['moar']]],
 'nested': {'dict': 'asdasd'},
 'normal': 'kskdaskad'}

I understand I could use the separator param and make it something really weird, but I am always running the risk of ruining my original dict structure if (heaven forbid) any of my key names include that same string pattern. Maybe quote encapsulation could help here?

Fixing this issue would make the library more plug-and-play since I am sure I am not the only one that likes to use underscores in their JSON key names.

rorosentrater avatar Oct 21 '19 15:10 rorosentrater

I've created a PR for allowing automatic replacing of separators if they are found in any keys. This might help with your issue (depending on what you need)

https://github.com/amirziai/flatten/pull/61

baolsen avatar Dec 24 '19 13:12 baolsen