0

i have a server in python that is recieving data in following format:

  MultiDict([('JSONdata', u'{"SMS":[{"inbox":[{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP *qqqqq*","phone":"+920000000":"BACKUP *qqqqq*","phone":"+920000000"},{"body":"BACKUP \\"qqqqq\\"    ","phone":"+920000000"},{"body":"BACKUP \\"qqqqq\\"   ","phone":"+920000000"},{"body":"BACKUP \\"qqqqq\\"  ","phone":"+920000000"},]},{"sent":[]}]}')])

i have written the following code to fetch it and convert it in JSONObject:

   request.POST.getall('JSONdata')
    incomingData = json.load(str)

but i am recieving the error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/router.py", line 251, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/router.py", line 227, in invoke_subrequest
    response = handle_request(request)
  File "/home/anam/cdsr/myproject/auth.py", line 50, in auth_request
    return handler(request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid_debugtoolbar/toolbar.py", line 130, in toolbar_tween
    return handler(request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/tweens.py", line 21, in excview_tween
    response = handler(request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 63, in tm_tween
    response = handler(request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 347, in rendered_view
    result = view(context, request)
  File "/home/anam/pyckenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 493, in _requestonly_view
    response = view(request)
  File "/home/anam/cdsr/myproject/controllers/controllers.py", line 44, in data
    incomingData = json.load(str)
  File "/usr/lib/python2.7/json/__init__.py", line 274, in load
    return loads(fp.read(),
AttributeError: 'list' object has no attribute 'read'
QMobile-A8 - - [11/Oct/2013 08:24:08] "POST /data HTTP/1.1" 500 59

i have tried different ways to solve it but nothings working.. ??? could someone tell me what am i doing wrong?

Ifrah
  • 209
  • 1
  • 5
  • 11

1 Answers1

0

Documentation says that load function

Deserialize a .read()-supporting file-like object containing a JSON document

and are giving it string instead.

You need loads (LOAD String) function from json module

alexvassel
  • 10,600
  • 2
  • 29
  • 31
  • thanks .. but after doing that error changed to ... File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer – Ifrah Oct 11 '13 at 12:35
  • edit question to show what is inside `MultiDict` instance, please and what does `request.POST.getall('JSONdata')` return – alexvassel Oct 11 '13 at 12:36
  • i have added complete incoming multidict – Ifrah Oct 11 '13 at 12:40
  • It is something wrong with your string. You should check its validity. – alexvassel Oct 11 '13 at 12:46
  • i have tried sending a simple JSONObject MultiDict([('JSONdata', u'{"name":"1234"}')]) still its giving error .. File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer – Ifrah Oct 11 '13 at 13:00