JSON object must be str, bytes or bytearray, not dict Ask Question

JSON object must be str, bytes or bytearray, not dict Ask Question

In Python 3, to load json previously saved like this:

json.dumps(dictionary)

the output is something like

{"('Hello',)": 6, "('Hi',)": 5}

when I use

json.loads({"('Hello',)": 6, "('Hi',)": 5})

it doesn't work, this happens:

TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

ベストアンサー1

json.loads take a string as input and returns a dictionary as output.

json.dumps take a dictionary as input and returns a string as output.


With json.loads({"('Hello',)": 6, "('Hi',)": 5}),

You are calling json.loads with a dictionary as input.

You can fix it as follows (though I'm not quite sure what's the point of that):

d1 = {"('Hello',)": 6, "('Hi',)": 5}
s1 = json.dumps(d1)
d2 = json.loads(s1)

おすすめ記事