4

I'm trying to use to use resource_stream from pkg_resources in conjunction with json.load and am having problem in python 3 that were not present in python 2.

When I try to run the following command, I get the error:

loaded_json = json.load(resource_stream(__name__, 'path/to/foo.json'))

>> TypeError: the JSON object must be str, not 'bytes'
Luke
  • 6,699
  • 13
  • 50
  • 88

1 Answers1

5

It looks like in Python 3, json.load no longer supports reading from a bytestream, you'll have to decode it before parsing it:

json_string = resource_stream(__name__, 'path/to/foo.json').read().decode()
loaded_json = json.loads(json_string)
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • This behavior exists in 3.5.2, but not in 3.6.7. Not sure what the exact point-version is where this was fixed, but in 3.6.7, you can use `json.load(resource_stream(...))` without having to read and decode. – Vivin Paliath Dec 13 '18 at 01:03