I have a text file temp.txt
of the sort --
{
"names" : [ {"index" : 0, "cards": "\n\nbingo" ...} ]
"more stuff": ...
}
{
"names" : [ {"index" : 0, "cards": "\nfalse" ...} ]
"more stuff": ...
}
.
.
Here's how I am trying to load it --
def read_op(filename):
with open("temp.txt", 'r') as file:
for line in file:
print (json.load(line))
return lines
But this throws the error:
JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1
I'm not sure what I am doing wrong here. Are there alternatives to reading this file another way?
Reading it line-by-line will not work because every line is not a valid JSON object by itself.
You should pre-process the data before loading it as a JSON, for example by doing the following:
[]
to contain the dataimport re
import json
with open(r'test.txt', 'r') as fp:
data = fp.read()
concat_data = re.sub(r"\}\n\{", "},{", data)
json_data_as_str = f"[{concat_data}]"
json_data = json.loads(json_data_as_str)
print(json_data)