Search code examples
pythonjsonspyderdata-analysis

JSONDecodeError: Unterminated string starting at Python even I tried with strict = False to neglect characters like /n


Here is my code :

import json
path = 'usagov_bitly_data2012-03-16-1331923249.txt'
print(open(path).readline())
records = [json.loads(line,strict=False) for line in open(path)]
print(records[0])

I got an error message as JSONDecodeError: Unterminated string starting at. I am using the json module to read the JSON file line by line using python.

I couldn't get the expected output. Can anyone help to solve this issue ?


Solution

  • Hi there try this,

    import json
    path = 'usagov_bitly_data2012-03-16-1331923249.txt'
    print(open(path).readline())
    records = [json.dumps(line) for line in open(path)]
    final_records = [json.loads(lines) for lines in records]
    print(final_records[0])