Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created September 10, 2013 17:46
Show Gist options
  • Save lsauer/6512956 to your computer and use it in GitHub Desktop.
Save lsauer/6512956 to your computer and use it in GitHub Desktop.
Python / json module - ValueError: Expecting property name: line 1 column 1 (char 0) or line 1 column 1 (char 1)

www.lsauer.com; 2013 lo sauer ###JSONDecodeError: Expecting value: line 1 column 1 (char 0) ###ValueError: Expecting property name ######Related to: http://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0 Trying to parse json in python. ValueError: Expecting property name http://stackoverflow.com/questions/9187885/trying-to-parse-json-in-python-valueerror-expecting-property-name

###Troubleshoot

Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.

In most cases your json.loads- JSONDecodeError: Expecting value: line 1 column 1 (char 0) error is due to :

non-JSON conforming quoting
XML/HTML output (that is, a string starting with <), or
incompatible character encoding

Ultimately the error tells you that at the very first position the string already doesn't conform to JSON.


As such, if parsing fails, despite having a data-body that looks at first glance JSON-like, try replacing the quotes of the data-body, as follows.

#www.lsauer.com, 2011 lo sauer
import sys, json, urllib, urllib2
url = 'https://www.googleapis.com/someapi'
urlparams = urllib.urlencode({'firstname': 'Foo','lastname': 'Karamba'})
urlheaders = {
'Host': 'univie.ac.at',
'Connection': 'keep-alive',
'Origin': 'http://www.univie.ac.at',
'User-Agent': 'XTRFAPIClient',
'Content-type': 'application/json',
'Accept': 'text/javascript, text/html, application/xml, text/xml, application/json */*',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Charset': 'utf-8',
}
request = urllib2.Request(url, urlparams, headers)
response_json = urllib2.urlopen(req).read()
struct = {}
try:
try: #try parsing to dict
dataform = str(response_json).strip("'<>()[]\"` ").replace('\'', '\"')
struct = json.loads(dataform)
except:
print repr(resonse_json)
print sys.exc_info()
#Note: Quotes within the data are presumed to be properly escaped
@tiger-flying
Copy link

I have just spent more than one hour to find out why my call of json.loads cause exception: "<type 'exceptions.ValueError'> Expecting property name", finally I found out that the error in my json string is the trailing comma, for example, if my string is: s = '{"a": 1, "b": 2,}', then because of the trailing comma(after b), the call of json.loads(s) will raise the "Expecting property name" exception. Note the trailing comma is widely used by C programmers, so I propose to add this kind of error to your common error list above.^_^

@m-sinclair
Copy link

@tiger-flying Just wanted to comment and say thanks! I was doing the same thing - had no idea the trailing comma was the culprit!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment