-1

Beginner Question beware

So basically I am using an API to get certain Data, in this case a dealID.

I am using following code:

resp = service.get_deal_data( size=1, level=None, limit_distance=None, limit_level=None)

Size, level etc do not matter.

So the api call returns this output:

{'date': '2022-09-20T13:35:42.042',
 'status': 'OPEN',
 'reason': 'SUCCESS',
 'dealReference': 'MKPYFDNLMCCTYNK',
 'dealId': 'DIAAAAKMH5HGMA8',
 'affectedDeals': [{'dealId': 'DIAAAAKMH5HGMA8', 'status': 'OPENED'}],
 'level': 0.99663,
 'size': 1.0,}

My question is how do I get the data from the output to use it in another function

I want to get the DealID and continue to use it without inputting manually.

F.e.:

I want to compare the deal ID to a list of IDs and return a warning if it already is on the list.

list = [DDHWUH5HGMA8,JNDWJ7HDJ2, 7HDWUDH9D, ...]

if dealId in list:
    print("exist")
else:
    print("not exist")

returns following error:

NameError: name 'dealId' is not defined

  • I see you have posted some code which assigns a value to `resp`. You don't show anything else which uses this variable. Is there some other code you can share with us? – quamrana Sep 20 '22 at 13:59
  • Ah I forgot to add that after assigning resp = service.get_deal... I am using just resp to use the function if that makes sense so the code is "resp = service.get_deal_data( size=1, level=None, limit_distance=None, limit_level=None)" "resp" – Nikolas Weichert Sep 20 '22 at 14:00
  • Also "print (resp[dealId])" gives the same name error – Nikolas Weichert Sep 20 '22 at 14:01
  • 1
    So, you've tried that? If so, you should update your question with that code, plus the full error traceback it produces. – quamrana Sep 20 '22 at 14:02
  • Use `'dealId'` instead. –  Sep 20 '22 at 14:02
  • A couple of things. 1. `list` is a reserved word in Python, so don't use it for variable names. 2. Once you get the response, you have to get the data you require from that. so here I am getting the deal id like `resp.get("dealId")`. This will only work is the returned data ie. response is of type Dict. If it's purely json string, then you have to `import json` and do something like `dict_data = json.loads(response)` – defiant Sep 20 '22 at 14:08
  • If you don't know what type a return value is, you can use `type()` function. in this case it seems to be a dictionary. If one day you have an unknown object and no doc, you can use `dir()` to list object's functions and attributes – Boop Sep 20 '22 at 14:09

1 Answers1

1

If the API result is the type of dictionary, then Try doing :

list = [DDHWUH5HGMA8,JNDWJ7HDJ2, 7HDWUDH9D, ...]

if resp['dealID'] in list:
    print("exist")
else:
    print("not exist")
Om Anand
  • 44
  • 1