Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Load a from a text file containing multiple JSONs into Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading