Skip to content
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

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

Open
rorosentrater opened this issue Oct 21, 2019 · 1 comment

Comments

@rorosentrater
Copy link

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.

@baolsen
Copy link

baolsen commented Dec 24, 2019

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)

#61

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

No branches or pull requests

2 participants