1

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?

3
  • 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 Commented Oct 6, 2022 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. Commented Oct 6, 2022 at 17:38
  • 1
    @RobertDodier you would still need to concatenate the commas , to separate each object inside the array. Commented Oct 6, 2022 at 17:43

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.