ewoodrich
ewoodrich

Reputation: 35

Django simplejson error

I'm attempting to use simplejson to parse a JSON string. For some reason, when I use simplejson.loads I recieve the following error:

ERROR:root:Exception in request:
Traceback (most recent call last):
File "C:\django\lokus_web_new\django\core\handlers\base.py", line 111, in get_
response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\django\lokus_web_new\mobile\views.py", line 13, in entry_ajax
test = simplejson.loads(entry_param)
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\__init__.py", line 388, in loads
return _default_decoder.decode(s)
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\decoder.py", line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\decoder.py", line 420, in raw_decode
raise JSONDecodeError("No JSON object could be decoded", s, idx)
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
INFO:root:"POST /entry_ajax/ HTTP/1.1" 500 -

The JSON string is being generated by the native javscript JSON.stringify function and is as follows:

{'type':'basic','id':'156','payload':{'text':'asd'}}

The following is the code of the view:

    def entry_ajax(request):
        entry_param = str(request.POST)
        test = simplejson.loads(entry_param)
        return HttpResponse(test['type'])  

Any thoughts?

Upvotes: 1

Views: 973

Answers (1)

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13486

This is not a valid JSON string. Use double quotes:

{"type":"basic","id":"156","payload":{"text":"asd"}}

Upvotes: 2

Related Questions