Python JSON loads error: ValueError: Extra data
I have a list of dictionaries that I dump and output to file:
output = call_the_articles() #Function Outputs a List of Dicts outfile = open("commentData.json", "w") #Also tried 'with open() as outfile:' form data = dumps(output) outfile.write(data) outfile.close()
But I can't use json.loads() to read the file, so either I'm encoding or decoding improperly(?) Example:
file = open("commentData.json", "r") json_data = json.loads(file.read())
ValueError: Extra data: line 1 column 28779 - line 1 column 69744 (char 28779 - 69744)
How should I store the list of dicts to file such that they can be decoded into JSON?
As stated in the json python doc
If the contents of fp are encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed, and should be wrapped with codecs.getreader(encoding)(fp), or simply decoded to a unicode object and passed to loads().
Try to specify in the argument list for load the same encoding used in CommendData.json.