Python Forum

Full Version: Name not found in response json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI,

Newbie here.
Not sure whats wrong with this code.
If anyone could help.
Basically what i want is, once entered a name of a selection I would like to see its ID.

import json
import requests

def post_to_ba(endpoint, raw_json):
    headers = {'Content-Type': 'application/json'}
    response = requests.post(endpoint, data=raw_json, headers=headers)
    return response.text

# Prompt the user for the data to search for
data = input("Enter the data to search for: ")

# Make the request and store the response in a variable
response_text = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets", '{"dataRequired":["ID","NAME","MARKET_START_TIME","EVENT_ID","EVENT_TYPE_ID","MARKET_TYPE","SELECTION_IDS","SELECTION_NAMES"]}')

# Parse the response text as JSON
response_json = json.loads(response_text)

# Search for the data in the response JSON object
if data in response_json:
    print(f"ID for {data}: {response_json[data]}")
else:
    print(f"{data} not found in the response JSON.")
Please post the response as text, not a screen shot. Better yet, print the response_json object and post that. I cannot see the entire response and don't know how to access the dictionaries.

"Hitched" will be a dictionary value, not a key. Somehow you have to get down to the list of id/name dictionaries. Then you'll have to loop through these and compare data to the value of name to find the one that matches.
id = None
for thing in thing_list:
    if thing["name"] == data:
        id = thing["id"]
        break
(Dec-28-2022, 10:13 AM)deanhystad Wrote: [ -> ]What does the response look like?

Hi there.
Check out the screenshot i attached to the original post.
(Dec-28-2022, 10:13 AM)deanhystad Wrote: [ -> ]Please post the response as text, not a screen shot. Better yet, print the response_json object and post that. I cannot see the entire response and don't know how to access the dictionaries.

"Hitched" will be a dictionary value, not a key. Somehow you have to get down to the list of id/name dictionaries. Then you'll have to loop through these and compare data to the value of name to find the one that matches.
id = None
for thing in thing_list:
    if thing["name"] == data:
        id = thing["id"]
        break

Output:
{ "status": "OK", "result": { "markets": [{ "id": "1.208041168", "name": "Kelso 29th Dec - 11:53 3m2f Hcap Chs", "marketType": "WIN", "eventId": "31988791_11:53", "eventTypeId": "7", "startTime": "2022-12-29T11:53:00+00:00", "selections": [{ "id": "38340860", "name": "Camp Belan" }, { "id": "17952", "name": "Smackwater Jack" }, { "id": "26888237", "name": "Rath An Iuir" }, { "id": "21092030", "name": "Hold The Note" }, { "id": "27925430", "name": "Morozov Cocktail" }, { "id": "12910829", "name": "Juge Et Parti" }, { "id": "10612411", "name": "Donnas Delight" }, { "id": "12327656", "name": "Glittering Love" }, { "id": "17610", "name": "Prince Dundee" }, { "id": "27351241", "name": "Thunderosa" }, { "id": "38863079", "name": "Along Long Story" }] }, { "id": "1.208041167", "name": "Kelso 29th Dec - 11:53 Each Way", "marketType": "EACH_WAY", "eventId": "31988791_11:53", "eventTypeId": "7", "startTime": "2022-12-29T11:53:00+00:00", "selections": [{ "id": "38340860", "name": "Camp Belan" }, { "id": "17952", "name": "Smackwater Jack" }, { "id": "26888237", "name": "Rath An Iuir" }, { "id": "21092030", "name": "Hold The Note" }, { "id": "27925430", "name": "Morozov Cocktail" }, { "id": "12910829", "name": "Juge Et Parti" }, { "id": "10612411", "name": "Donnas Delight" }, { "id": "12327656", "name": "Glittering Love" }, { "id": "17610", "name": "Prince Dundee" }, { "id": "27351241", "name": "Thunderosa" }, { "id": "38863079", "name": "Along Long Story" }] }, { "id": "1.208041095", "name": "Doncaster 29th Dec - 14:40 Each Way", "marketType": "EACH_WAY", "eventId": "31988788_14:40", "eventTypeId": "7", "startTime": "2022-12-29T14:40:00+00:00", "selections": [{ "id": "25781439", "name": "Gesskille" }, { "id": "22046306", "name": "Dublin Four" }, { "id": "16149309", "name": "Storm Control" }, { "id": "38402051", "name": "Omar Maretti" }, { "id": "35808426", "name": "Burrows Diamond" }, { "id": "18302519", "name": "Boldmere" }, { "id": "24044532", "name": "Coeur Serein" }, { "id": "17194935", "name": "Senior Citizen" }, { "id": "12244761", "name": "Dead Right" }, { "id": "22664026", "name": "One More Fleurie" }] }] } }
Will this help ?
(Dec-28-2022, 09:53 AM)NewbiePyt Wrote: [ -> ]Not sure whats wrong with this code.
Your code checks if user input (e.g. Hitched) is present as key at top-level of this nested dictionary.

for market in response_json['result']['markets']:
    for item in market['selections']:
        if item['name'] == data:
            print(item['id'])