Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONDecodeError: Expecting value: line 1 column 1

Tags:

I am receiving this error in Python 3.5.1.

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is my code:

import json import urllib.request  connection = urllib.request.urlopen('http://python-data.dr-chuck.net/comments_220996.json')  js = connection.read()  print(js)  info = json.loads(str(js)) 

image

like image 600
beeny Avatar asked Jan 03 '16 17:01

beeny


People also ask

What does Expecting value line 1 column 1 char 0 mean Python?

The Python "json. decoder. JSONDecodeError: Expecting value: line 1 column 1 (char 0)" occurs when we try to parse something that is not valid JSON as if it were. To solve the error, make sure the response or the file is not empty or conditionally check for the content type before parsing.

How to fix JSONDecodeError Expecting value?

You can resolve this error by checking the JSON file or string for valid content and using an if-statement when making a RESTful call to ensure the status code is 200 and the content type is application/json .

How to Solve JSONDecodeError?

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.


1 Answers

If you look at the output you receive from print() and also in your Traceback, you'll see the value you get back is not a string, it's a bytes object (prefixed by b):

b'{\n  "note":"This file    ..... 

If you fetch the URL using a tool such as curl -v, you will see that the content type is

Content-Type: application/json; charset=utf-8 

So it's JSON, encoded as UTF-8, and Python is considering it a byte stream, not a simple string. In order to parse this, you need to convert it into a string first.

Change the last line of code to this:

info = json.loads(js.decode("utf-8")) 
like image 82
Dan Lowe Avatar answered Sep 22 '22 02:09

Dan Lowe