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

unflatten does not work when importing JSON from file #34

Open
alexabbott opened this issue Aug 7, 2018 · 3 comments
Open

unflatten does not work when importing JSON from file #34

alexabbott opened this issue Aug 7, 2018 · 3 comments

Comments

@alexabbott
Copy link

when using the code below and attached file
data.json.zip, unflatten does not work. Shows error of 'provided dictionary is not flat'

import yaml,json
from flatten_json import unflatten

json_file = open('data.json')
json_str = json_file.read()
json_data = json.loads(json_str)
unflatten(json_data)
@amirziai
Copy link
Owner

amirziai commented Aug 8, 2018

do you have the latest version of flatten_json? try upgrading and try again

pip install --upgrade flatten_json

i'm get this output with your example:

{'seo': {'test': 'hello'}}

@alexabbott
Copy link
Author

alexabbott commented Aug 13, 2018

I have the latest version of flatten_json installed. I'm running Python 2.7, pip 9.0.1

Still getting the same error...

Uncommenting these lines from the library allows me to run the command without error:

def _unflatten_asserts(flat_dict, separator):
    assert isinstance(flat_dict, dict), "un_flatten requires a dictionary input"
    assert isinstance(separator, str), "separator must be a string"
    # assert all((not isinstance(value, Iterable) or isinstance(value, str)
                # for value in flat_dict.values())), "provided dictionary is not flat"

@stevegoossens
Copy link

stevegoossens commented Dec 19, 2018

Is this a unicode and Python 2 issue? Looks like it to me. My guess would be that people who don't experience this issue are using Python 3....

e.g. on python 2.7

>>> config = {u'mfa': u'off', u'datadog.apm_config.enabled': u'true'}
>>> unflatten(config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/flatten_json.py", line 79, in unflatten
    _unflatten_asserts(flat_dict, separator)
  File "/usr/local/lib/python2.7/site-packages/flatten_json.py", line 68, in _unflatten_asserts
    for value in flat_dict.values())), "provided dictionary is not flat"
AssertionError: provided dictionary is not flat
>>> config['mfa']
u'off'
>>> import json
>>> json.dumps(config, indent=2)
'{\n  "mfa": "off", \n  "datadog.apm_config.enabled": "true"\n}'
>>> print json.dumps(config, indent=2)
{
  "mfa": "off",
  "datadog.apm_config.enabled": "true"
}

a workaround in my case is to cast the keys and values to strings (they're both always strings in my scenario) and then the assertion passes:

>>> config_as_str = { str(k): str(v) for k, v in config.iteritems() }
>>> config_as_str
{'mfa': 'off', 'datadog.apm_config.enabled': 'true'}
>>> unflatten(config_as_str, separator='.')
{'mfa': 'off', 'datadog': {'apm_config': {'enabled': 'true'}}}

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

3 participants