how to read json object in python
how to read json object in python
You should pass the file contents (i.e. a string) to json.loads()
, not the file object itself. Try this:
with open(file_path) as f:
data = json.loads(f.read())
print(data[0][text])
Theres also the json.load()
function which accepts a file object and does the f.read()
part for you under the hood.
Use json.load()
, not json.loads()
, if your input is a file-like object (such as a TextIOWrapper).
Given the following complete reproducer:
import json, tempfile
with tempfile.NamedTemporaryFile() as f:
f.write(b{text: success}); f.flush()
with open(f.name,r) as lst:
b = json.load(lst)
print(b[text])
…the output is success
.