Load a from a text file containing multiple JSONs into Python

Question:

I have a text file temp.txt of the sort —

{
 "names" : [ {"index" : 0, "cards": "nnbingo" ...} ]
 "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?

Asked By: Rahul Dev

||

Answers:

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'/Users/sidfeiner/work/pierate/data-scripts/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)
Answered By: sid802
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.