As a result of reading the json file, python gives the error:

Traceback (most recent call last): File "C:\Users\guest_user\Documents\test json code.py", line 8, in <module> fgh = json.loads(data) File "C:\Python27\lib\json\__init__.py", line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded 

Script:

 # -*- coding: utf-8 -*- import json import sys, codecs import io with open('top100.json') as f: data = f.read() fgh = json.loads(data) print(fgh) 

The file is saved in unicode format, utf-8 does not help. File content is valid:

 [[1, "joke1"], [2, "шутка"]] 

Tell me how to solve the problem?

  • Put a JSON file somewhere to help reproduce the problem. If I create a file with the specified content - I have your code working correctly - MaxU
  • Link to download file yadi.sk/d/PXaA_LMA37H82H - bvz

3 answers 3

Usually this error indicates that the JSON file contains errors (invalid).

How to check (example) - run in the command line:

 python -m json.tool a.json Expecting value: line 1 column 34 (char 33) 

a.json:

 [[1, "joke1"], [2, "шутка"],] # ^ # | 

PS I specifically added a comma to show how json.tool works

  • MaxU, thanks for the comment. After running on the command line, the result is: 'utf-8' codec can't decode byte 0xf8 in position 0: invalid start byte. This is if the file is saved in the ANSII encoding. If you save in utf-8, it gives No Json object could be decoded. Why does not recognize the file is not clear, the content exactly matches the line [[1, "joke1"], [2, "joke"]] - bvz

Reinstalling python 2.7 partially solved the problem. Also managed to find out what the problem is in Cyrillic. If the file is changed like this:

 [[1, "joke1"], [2, "joke"]] 

then everything is recognized correctly, if you leave the Cyrillic alphabet, then the error:

 Traceback (most recent call last): File "C:\Users\guest_user\Documents\test json code.py", line 8, in <module> **fgh = json.loads(data)** File "C:\Python27\lib\json\__init__.py", line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) UnicodeDecodeError: 'utf8' codec can't decode byte 0xf8 in position 0: invalid start byte 

The following code executes:

 import sys import json import codecs import io with codecs.open('top100.json') as f: data = f.read().decode("cp1251", "replace") print(data) fgh = json.loads(data) print(fgh) 

But Russian characters do not display:

 [[1, u'joke1'], [2, u'\u0448\u0443\u0442\u043a\u0430']] 

Can you please tell me how to fix u '\ u0448 \ u0443 \ u0442 \ u043a \ u0430' into Russian?

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf8 in position 0: invalid start byte

The problem with you is that the file with json text is saved using cp1251 encoding, and json.loads() by default expects utf-8 encoding.

 #!/usr/bin/env python import json import io with io.open('top100.json', encoding='cp1251') as file: data = json.load(file) 

It is better to use utf-8 for json text to support any Unicode characters.