0

I'm just starting with web2py and python, I am trying to utilize a rest web service using Requests, the call goes through ok and I get the appropriate data back but I am having trouble finding information on how to parse the response to a field. i.e. assigning customer name to a variable. Most examples just print the .json() return data.

import requests
Controller Code:
url = 'http://services/myRestService/OrderInformation/?$top=1'
r = requests.get(url,headers = headers, auth=('testuser','testuser'))
results = r.json()
customername = ?

Results:
{u'd': {u'results': [{u'OrderNumber': u'     159', u'__metadata': {u'type':   u'OrderInformation', u'id': u"http://127.0.0.1/services/OrderInformation('%20%20%20%20%20159')", u'uri': u"http://127.0.0.1/services/OrderInformation('%20%20%20%20%20159')"}, u'OrderDate': u'4/12/2012 12:00:00 AM', u'CustomerNumber': u'901                 ', u'CustomerName': u’Coffee Outlet                          '}], u'__next': u'http://127.0.0.1/services/OrderInformation/?$select=CustomerName,CustomerNumber,OrderNumber,OrderDate%20'}}
cmhdev
  • 43
  • 8

2 Answers2

0

The json seem truncated. What Guarav writes in the comments is to get the first customer in the results list and fetch the CustomerName from the data structure.

See the python2 or python3 documentation on data structures. Or google for "python dictionaries". It will explain in detail how to handle these very useful data structures.

Remco
  • 435
  • 3
  • 10
0

First of all the json you are showing is surely not complete(Missing the closing brackets or it has some more data).

This may be problem on the server side(highly unlikely). If it is you cannot do anything of it unless you have made that server.

Provided that it is your mistake in copy-pasting the json - Your question may be fairly easy and can be solved by the answer provided by Gaurav.

If your question is to remove the white spaces from the keys of the dict you can remove them by using python map. Something like map(lambda x: x.strip(), yourList) can be done.

Maybe provide some concrete and accurate details for your problem.

Hope this helps!!!

raj454raj
  • 299
  • 3
  • 12
  • Yes something went awry in copying. I updated the results in the text above and on my WS call I selected several fields instead of the entire list to make the response smaller. Thanks for your response – cmhdev Oct 25 '15 at 02:35
  • Do you still have any questions? – raj454raj Oct 25 '15 at 17:17
  • I have had good responses to my questions. thanks all for your help. – cmhdev Oct 25 '15 at 17:20