0

I am stuck here again... I have a file named "data.json" and I want to open it with python but I am getting errors.

import json
>>> data=json.load(open("data.json"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 340,
in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 4912995)
>>>
Angel
  • 49
  • 4
  • Do you have 2 or more than 2 records in your json file? – Muzzamil Jan 18 '20 at 20:34
  • Your JSON file may be malformed or have an encoding issue, if you can provide your JSON file that would help. Or look for a JSON formatter online, copy it and it will tell you if it is valid. Also, I recommend specifying the mode in the open call, i.e. open("data.json", "r") – Marc Sances Jan 18 '20 at 20:35
  • @MarcSances i did specify the mode in the open call and still errors. I don't know jack about this stuff as i am still learner – Angel Jan 18 '20 at 20:42
  • 1
    @Muzzamil i don't really know – Angel Jan 18 '20 at 20:42
  • Please provide your JSON file so we can see if it's valid or not. – Marc Sances Jan 18 '20 at 20:43
  • Please share json file’s content in question if possible – Muzzamil Jan 18 '20 at 20:44
  • @MarcSances okay – Angel Jan 18 '20 at 20:44
  • Does this answer your question? [json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190)](https://stackoverflow.com/questions/48140858/json-decoder-jsondecodeerror-extra-data-line-2-column-1-char-190) – DanielM Jan 18 '20 at 20:46
  • Most likely you have 2 records in one json file, check https://stackoverflow.com/questions/48140858/json-decoder-jsondecodeerror-extra-data-line-2-column-1-char-190 – DanielM Jan 18 '20 at 20:46
  • @DanielM i am just confused about the records stuff... do i reformat the jason file? if yes, how do i do that? – Angel Jan 18 '20 at 20:49
  • @Angel reformat as i mentioned in my answer – Muzzamil Jan 18 '20 at 21:04

2 Answers2

2

According to Python JSON documentation

If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.

Not knowing the content of your file, it is hard to say what is wrong, but I would suspect that text in your file is not a valid JSON object, or more likely (according to "Extra data" search, answered here) the file "data.json" includes more than one JSON object.

For example, using your code: This file works correctly

{ "name":"John", "age":30, "car":null }

but this one

{ "name":"John", "age":30, "car":null }
{ "name":"John", "age":30, "car":null }

throws the same errors

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", 
line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", 
line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", 
line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 6 column 1 (char 55)
0

In case 2 or more than 2 record, you have to reformat your file as mentioned below OR you have to load file record by record.

You need to reformat your json to contain an array like below:

{
    "foo" : [
       {"name": "XYZ", "address": "54.7168,94.0215", "country_of_residence": "PQR", "countries": "LMN;PQRST", "date": "28-AUG-2008", "type": null},
       {"name": "OLMS", "address": null, "country_of_residence": null, "countries": "Not identified;No", "date": "23-FEB-2017", "type": null}
    ]
}
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • the json module is a standard module that came with python install. the data.json file was download and i want to open it with python – Angel Jan 18 '20 at 21:00
  • Yes. Can you reformat json data/ file as array and give it try to load. – Muzzamil Jan 18 '20 at 21:02
  • Thanks everyone....i was able to load the file successfully. how i did it was that, the data.json file contains some urls and other codes that was not related of which i deleted and was able to open it – Angel Jan 18 '20 at 21:06
  • thanks for your support, please can you recommend a good tutorials to learn all this? – Angel Jan 18 '20 at 21:06
  • Perfect. You can learn basics from here https://www.learnpython.org – Muzzamil Jan 18 '20 at 21:08