Home > other >  Load a from a text file containing multiple JSONs into Python
Load a from a text file containing multiple JSONs into Python

Time:10-07

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?

CodePudding user response:

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:

  1. Read the whole content
  2. Add commas between every 2 objects
  3. Add [] to contain the data
  4. Load with json.loads
import 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)
  • Related