ValueError: No JSON object could be decoded

449 views
Skip to first unread message

Dhaval M

unread,
Jun 21, 2015, 9:23:50 AM6/21/15
to django...@googlegroups.com
Thank you in advance for any help (this is my first post to this community)

I am using Django as my application server for my project. I am new to web development so please pardon and feel free to correct me for any mistake.

I am running simple, client server combination where from client I am trying to send JSON data and on the server I am trying to parse it.

SERVER CODE:

def addPatient(request):

     if request.method == 'POST':

        # Convert JSON to python objects and

        # store into the DB

        recJSON = json.loads(request.body)

        #logger.debug("%s",recJSON['SenderID'])

        logger.debug("%s",request.body)


        #print 'Raw Json "%s"' % request.body

        return HttpResponse(json.dumps(request.body),content_type="application/json")

        


But getting an error at json.loads. ValueError: No JSON object could be decoded 


Also I would appreciate if any one can direct me to good tutorial on Django (I already read http://www.djangobook.com/)


I dont know where and what I am doing wrong. I checked the client side message and it is JSON used http://jsonlint.com/ to validate it is JSON data.


Thank you once again.

Vijay Khemlani

unread,
Jun 21, 2015, 11:14:17 AM6/21/15
to django...@googlegroups.com
What is the actual content of request.body? (as in, "print request.body")

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5bc51870-6b56-456f-bc6d-18cf20487265%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bill Freeman

unread,
Jun 22, 2015, 9:06:30 AM6/22/15
to django-users
You probably don't want request.body.  You are probably POSTing the JSON using a form, which means that it shows up as something like request.POST['data'], where you should replace 'data' with the name of the form element (textarea?) where you are putting the JSON.  Posting with a form wraps things up in a multipart form (allowing you to have, among other things, more than one field in a post), and that extra stuff doesn't look line JSON, but the whole is what you get when you use request.body.

Reply all
Reply to author
Forward
0 new messages