40

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}
Nihal
  • 5,262
  • 7
  • 23
  • 41
SMS
  • 401
  • 1
  • 4
  • 5

3 Answers3

58

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.

Hannu
  • 11,685
  • 4
  • 35
  • 51
2

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 :)

Richard
  • 229
  • 1
  • 2
  • 5
  • This demonstrably has nothing to do with the question that was asked. The input data is not "malformed"; it is in a perfectly standard format. There was no reference in the question to an API call. It seems more like you are sharing a personal experience with the same error message. Please keep in mind that this is **not a discussion forum**. – Karl Knechtel Feb 04 '23 at 17:35
1

This error can also show up if there are parts in your string that json.loads() does not recognize. An in this example string, an error will be raised at character 27 (char 27).

string = """[{"Item1": "One", "Item2": False}, {"Item3": "Three"}]"""

My solution to this would be to use the string.replace() to convert these items to a string:

import json

string = """[{"Item1": "One", "Item2": False}, {"Item3": "Three"}]"""

string = string.replace("False", '"False"')

dict_list = json.loads(string)
Neil
  • 49
  • 5
  • 2
    This is because `False` is [not a valid boolean, according to JSON schema](http://json-schema.org/understanding-json-schema/reference/boolean.html). The correct boolean you are looking for is `false`, not `"False"` (which is a string). So, in your case, you should use `string.replace("False", "false")`. – Honza Zíka Aug 27 '21 at 08:52
  • **This answer is wrong**. The example will give a **different error**. – Karl Knechtel Feb 04 '23 at 08:23