This is the first time I try to use a JSON file in Python and although I have read a lot of stuff regarding this matter, I am still very confused. I want to read a file named jason.json line by line, store it to a list named data and then print it. However, i always get the following error :
Traceback (most recent call last):
File "try.py", line 6, in <module>
data.append(json.loads(line))
File "C:\Users\...\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\...\Python35\lib\json\__init__.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\...\Python35\lib\json\__init__.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
This is my code:
import json
data = []
with open('jason.json') as f:
for line in f:
data.append(json.loads(line))
print(data)
And this is jason.json:
{
"E(2,3)" : "A common Afro-Cuban drum pattern",
"E(2,5)" : "A rhythm found in Greece",
"E(3,4)" : "It is the archetypal pattern of the Cumbia from Colombia",
"E(3,5)" : "Persian rhythm"
}
Thank you in advance!
json.dump(my_data, my_file)it will no have line breaks, and you could (though not advised) load it using your approach.json.dump(my_data, my_file, indent=4)for example. JSON was never designed to be treated as a single-line format, really.