0

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?

Rahul Dev
  • 602
  • 1
  • 6
  • 16
  • That's not a JSON file - it contains many concatenated JSON structures. If it's not a large file, you should be able to break it up manually – inspectorG4dget Oct 06 '22 at 17:38
  • Well, my advice would be to load the whole file at once and then work with the resulting blobs as JSON objects (I guess whatever is returned by the JSON parser). I think JSON requires the whole file to be just one object, so maybe an easy way is to read the entire file content as a string, concatenate `[` and `]` to the ends, and then parse the result as a JSON array. – Robert Dodier Oct 06 '22 at 17:38
  • 1
    @RobertDodier you would still need to concatenate the commas `,` to separate each object inside the array. – Carl HR Oct 06 '22 at 17:43

1 Answers1

2

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)
sid802
  • 315
  • 2
  • 18