Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) [duplicate]

I am running the following code-

import json  addrsfile =  open("C:\\Users\file.json",  "r") addrJson = json.loads(addrsfile.read()) addrsfile.close() if addrJson:     print("yes") 

But giving me following error-

Traceback (most recent call last):   File "C:/Users/Mayur/Documents/WebPython/Python_WebServices/test.py", line 9, in <module>     addrJson = json.loads(addrsfile.read())   File "C:\Users\Mayur\Anaconda3\lib\json\__init__.py", line 354, in loads     return _default_decoder.decode(s)   File "C:\Users\Mayur\Anaconda3\lib\json\decoder.py", line 342, in decode     raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) 

Anyone help me please?

JSON file is like-

{"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} 
like image 685
SMS Avatar asked Jan 07 '18 19:01

SMS


People also ask

How do I fix JSON decode error?

The Python "json. decoder. JSONDecodeError: Extra data" occurs when we try to parse multiple objects without wrapping them in an array. To solve the error, wrap the JSON objects in an array or declare a new property that points to an array value that contains the objects.

What is the difference between JSON load and loads?

The json. load() is used to read the JSON document from file and The json. loads() is used to convert the JSON String document into the Python dictionary. fp file pointer used to read a text file, binary file or a JSON file that contains a JSON document.

What is JSON loads in Python?

loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.


2 Answers

You have two records in your json file, and json.loads() is not able to decode more than one. You need to do it record by record.

See Python json.loads shows ValueError: Extra data

OR you need to reformat your json to contain an array:

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

would be acceptable again. But there cannot be several top level objects.

like image 86
Hannu Avatar answered Sep 20 '22 19:09

Hannu


I was parsing JSON from a REST API call and got this error. It turns out the API had become "fussier" (eg about order of parameters etc) and so was returning malformed results. Check that you are getting what you expect :)

like image 23
Richard Avatar answered Sep 24 '22 19:09

Richard