pythonjsondpath

Python dpath.util.get Empty string keys Error


I read in a simple JSON text file and it parses to a dict just fine.

>>> data.keys()
dict_keys(['metadata', 'value'])

I want to get specific elements and I typically use the dpath package. However, in this case i get an error which seems to imply that I

dpath.util.get(data, 'metadata', separator='..')
InvalidKeyName: Empty string keys not allowed without dpath.options.ALLOW_EMPTY_STRING_KEYS=True

I don't see any empty string keys, only the two above. I can reproduce with some other seemingly random JSON text files but for others it works just fine. Any idea what is going on here?


Solution

  • Searching this error message in the library's codebase finds dpath/path.py:88:

        for (k, v) in iteritems:
            if issubclass(k.__class__, (string_class)):
                if (not k) and (not dpath.options.ALLOW_EMPTY_STRING_KEYS):
                    raise dpath.exceptions.InvalidKeyName("Empty string keys not allowed without "
                                                          "dpath.options.ALLOW_EMPTY_STRING_KEYS=True")
    

    So, this error is raised when your data structure has empty keys.