JSON object must be str, not 'bytes'

Using Python 3.5.1, I pulled out a text file where each line is in the form of JSON: {"a": "windows", "b": "stairs" ...}

import json path = 'folder/data.txt' records=[json.loads(line) for line in open(path,'rb')] 

But I got the error:

 the JSON object must be str, not 'bytes' 

I have no problem printing the first line of the file, so I'm sure the path to the file is correct.

+6
source share
3 answers

Open the file in text mode, not in binary mode (perhaps explicitly pass encoding='utf-8' to override the default system, since JSON is usually stored as UTF-8). The json module only accepts str input; reading from a file opened in binary mode returns bytes objects:

 # Using with statement just for good form; in this case it would work the # same on CPython, but on other interpreters or different CPython use cases, # it easy to screw something up; use with statements all the time to build good habits with open(path, encoding='utf-8') as f: records=[json.loads(line) for line in f] 
+3
source

Try: record = [json.loads (string. Decode () ) for a string in open (path, 'rb')]

+2
source

You do not want to specify "rb" because the binary representation of the file cannot be read by the JSON module. You probably want to encode "utf-8" and "read". EDIT: I initially said that both of these defaults, but I was informed that many operating systems have different default encodings and that Python uses the default system parameter in open (). Therefore, I would recommend setting the encoding parameter explicitly as "utf-8".

json supports loading from an open file using "json.load" instead of "json.loads", which is loaded from a line, so we can skip the read-as-how text and go directly to JSON. I do not think that you will want to “load” individual lines, as this will probably not be valid JSON.

 import json # open has __enter__ and __exit__ functions, so we can call it as a guard # using "with" syntax and it'll close when the scope ends with open(r".\myjson.json", encoding="utf-8") as fh: # load() is a convenience function to help us avoid iterating lines # on our own. It calls loads() on the whole doc and returns an obj json_obj = json.load(fh) print (json_obj) 
+1
source

All Articles