Problem code:

def check_mail(self, mail): f = open("base.json", 'r') text = f.read() result = json.loads(text) return result 

And the conclusion:

 Traceback (most recent call last): File "/Users/Vadim/Documents/Python/Houme Task/reg.py", line 42, in <module> print(data.check_mail("sokle@gmail")) File "/Users/Vadim/Documents/Python/Houme Task/reg.py", line 32, in check_mail return json.loads(text) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads return _default_decoder.decode(s) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 73 (char 72) 

MacOS / CPython 3.5

The data is written to the file in the following format:

 {"sokle@gmail.com": ["Pavel", "nejcenwvewnvlewn"]} 
  • It looks like something with json th, can I see it? - Arnial
  • the text in the example corresponds to what the error indicates ...line 1 column 73 (char 72) ? If this is possible, give the exact line to which json.loads is cursing - approximatenumber
  • shoo your json at jsonlint.com - JK_Action
  • Validation passes - Vadim Vova
  • 2
    The given json in question does not throw out any errors. Give a minimal json example that leads to an error. Throw one half of the file, check that the error remains, if not, then throw the other half, check that the error remains, etc., then save the result to the invalid.json file and show the result: print(open("invalid.json", "rb").read()) - jfs print(open("invalid.json", "rb").read())

4 answers 4

Perhaps the reason is that you are trying to decode multiple JSON ?
Works:

 >>> import json >>> json.loads('{}') {} 

But:

 >>> json.loads('{}{}') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> json.loads('{}{}') File "S:\Python\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) File "S:\Python\lib\json\decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) 
  • I tried to open json wikipedia unfortunately. The error is not changed. The problem is in the encoding, but I do not know how to recode. Applied decode to the file when opened, does not help. - Vadim Vova

Most likely it is in the file encoding. Read extra characters or invisible characters ( similar to UTF-8 BOM )

Here is what we managed to catch when working with the line before:

 >>> result = json.loads(text) 'п»ї{"sokle@gmail.com": ["Pavel", "nejcenwvewnvlewn"]}' Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python35\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\Python35\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python35\lib\json\decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

And the line after:

 >>> text '{"sokle@gmail.com": ["Pavel", "nejcenwvewnvlewn"]}vdsv' >>> result = json.loads(text) Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python35\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\Python35\lib\json\decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 51 (char 50) 

Hence, it looks like third-party characters in the data, which may not be visible in the notepad or notepad ++.

Next, the question arises of clipping these invisible characters. Most likely, it is necessary either to parse everything between the first "{" and the last "}" or to get the correct answer from the person who sends it.

    Use a better solution:

     import json with open("base.json") as json_file: json_data = json.load(json_file) print(json_data) 

      The problem was that I added the second dictionary in json, because of this I did not open it. The problem was solved when I created the list and started adding dictionaries with data to it. Thank you all for your hard work!

      • 3
        Those. in fact, the correct answer is the Other answer (you had multiple json). - insolor