json – Python TypeError: expected string or buffer

json – Python TypeError: expected string or buffer

You are trying to load a file object, when json.loads expects a string. You could either use

z = json.loads(open(test, r).read())

or, much better:

with open(test) as f:
    z = json.load(f)

In the first example, the file is opened, but never closed (bad practice). In the second example, the context manager closes the file after leaving the context block.

json – Python TypeError: expected string or buffer

Leave a Reply

Your email address will not be published.