Python Forum
Name not found in response json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Name not found in response json
#1
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.")

Attached Files

Thumbnail(s)
   
Reply
#2
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
Reply
#3
(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.
Reply
#4
(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 ?
buran write Dec-29-2022, 11:13 AM:
I pretty-formatted the JSON to better see the structure
Reply
#5
(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'])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,431 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  JSON response from REST service get nested value nl2ttl 2 2,542 Nov-30-2020, 09:34 PM
Last Post: nl2ttl
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,557 Nov-23-2020, 05:15 PM
Last Post: cananb
  parser json response absolut 4 2,853 Sep-15-2020, 12:10 PM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,088 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  API JSON response missing list gives keyerror rolfmadsen 3 3,482 Mar-28-2020, 10:12 AM
Last Post: buran
  How to process JSON response from requests? Heinrich 5 4,052 Jan-10-2020, 06:17 PM
Last Post: Heinrich
  Split of key from JSON response aswini_dubey 1 2,419 Dec-04-2019, 06:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020