1

I want to parse a json file in python. I don't know the content of the file. I downloaded this file from a website in json format.

As per my knowledge to parse a json file we need this code

import json

sourcefile=open("News_Category_Dataset_v2.json","r")
json_data=json.load(sourcefile)

print (json_data)

But I got this error as describe below. jsonparse.py is my file name which is save in my computer d:/algorithm

D:\python\envs\algorithms\python.exe D:/algorithms/jsonparse.py Traceback (most recent call last):

File "D:/algorithms/jsonparse.py", line 4, in <module>
json_data=json.load(sourcefile)

File "D:\python\envs\algorithms\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

File "D:\python\envs\algorithms\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)

File "D:\python\envs\algorithms\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 366)

Process finished with exit code 1

How could I fix the problem?

8
  • 1
    Your file is not valid JSON. Looking at the position of the problem, it might be JSON Lines where every line is a JSON object. It that possible? Commented Apr 24, 2019 at 4:26
  • Can you paste the contents of your json file? Commented Apr 24, 2019 at 4:31
  • @Nithin This json file downloaded. So I don't know it's content. How could I open a json file without parse it in python. My computer does not have any app which open json file. Commented Apr 24, 2019 at 4:34
  • 1
    I managed to find your the json file. It is not in a proper json format. The following code snippet might help you import json json_list = [] for i in open('test.json'): json_line = json.loads(i) json_list.append(json_line) print(json_list) Commented Apr 24, 2019 at 4:43
  • 1
    json file can be opened in any editor. Commented Apr 24, 2019 at 4:47

1 Answer 1

4

Your file is not json. but it has lines where each one of them is json.

This snippet should help you

import json

json_list = []
for i in open('test.json'):
    json_line = json.loads(i)
    json_list.append(json_line)
print(json_list)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.