Search code examples
pythonodata

ODATA, JSON and Python


I am stuck on this problem I would like to request this URL, which is in a odata format

"http://services.odata.org/V3/OData/OData.svc/Products?$format=json"

and that comes from this link. I tried with the following code but it doesn't work

from odata import ODataService

url_test = "http://services.odata.org/V3/OData/OData.svc/Products?
    $format=json"

service = ODataService(url, reflect_entities=False)

service.entities['ID']

My end goal is to retrieve all the different ID with their Price.

Thanks

I have this error message

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-2db727483d6e> in <module>()
      3 service
      4 
----> 5 service.entities['ID']

KeyError: 'ID'

Solution

  • I do not really know the module OData. It seems to be deprecated, abandoned or similar as you could see on the Pypi repository (link). Anyways, have you tried retrieving the data using requests?

    import requests
    
    url_test = 'http://services.odata.org/V3/OData/OData.svc/Products?$format=json'
    
    r = requests.get(url_test)
    
    r_json = r.json()
    for value in r_json['value']:
        print(value['ID'])
    

    This seems to work for me.