I checked how to parse the JSON string written to the file with Python, so make a note.
json.load ()
)For such a file
data.json
{
"data":{
"a": "Hello",
"b": 123
},
"c": true
}
JSON parsing can be done by using ** json.load ()
** for the data acquired by ʻopen ()`.
import json
raw = open('data.json', 'r')
type(raw)
# <class '_io.TextIOWrapper'>
parsed = json.load(raw)
type(parsed)
# <class 'dict'>
parsed
# {'data': {'a': 'Hello', 'b': 123}, 'c': True}
print(json.dumps(parsed, ensure_ascii=False, indent=4))
#{
# "data":{
# "a": "Hello",
# "b": 123
# },
# "c": true
#}
json.loads
)-- json.loads
cannot parse TextIOWrapper type data.
json.loads(raw)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/usr/lib64/python3.6/json/__init__.py", line 348, in loads
# 'not {!r}'.format(s.__class__.__name__))
# TypeError: the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'
that's all
Recommended Posts