Search code examples
pythonjsonjsonlines

Extracting text from json file and saving into text file


    import json
    file= open('webtext.txt','a+')
    
    with open('output-dataset_v1_webtext.test.jsonl') as json_file:
         data= json.load(json_file)
         for item in data:
         file.write(item)
         print(item)
    
    
 
>>> I am getting this error:
    
        raise JSONDecodeError("Extra data", s, end)
    json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 656)

I have already tried with json.loads()

My json file look like with multiple objects:

{"id": 255000, "ended": true, "length": 134, "text": "Is this restaurant fami"}
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}

Any advise will be highly appreciated on how to resolve the existing issue and write the dict['text'] into text file


Solution

  • you need to loop through it:

    import json
    
    
    with open('output-dataset_v1_webtext.test.jsonl','r') as json_file:
        for line in json_file.readlines():
             data= json.loads(line)
             for item in data:
                print(item)