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)
>>>
11
  • Do you have 2 or more than 2 records in your json file? Commented Jan 18, 2020 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") Commented Jan 18, 2020 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 Commented Jan 18, 2020 at 20:42
  • 1
    @Muzzamil i don't really know Commented Jan 18, 2020 at 20:42
  • Please provide your JSON file so we can see if it's valid or not. Commented Jan 18, 2020 at 20:43

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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}
    ]
}

5 Comments

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
Yes. Can you reformat json data/ file as array and give it try to load.
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
thanks for your support, please can you recommend a good tutorials to learn all this?
Perfect. You can learn basics from here learnpython.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.