-3

I made an API request:

# set up the request parameters
params = {
  'api_key': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
  'type': 'product',
  'item_id': 'XXXXXXXXX',
  'url': 'https://www.somestore.com',
  'output': 'json'
}

# make the http GET request to the API
api_result = requests.get('https://api.shopperapi.com/request', params)

product_data = json.loads(api_result.text)


if ((product_data["product.buybox_winner.seller.name"]=="MyStore.com") and (product_data["product.buybox_winner.availability.in_stock"]=="False")):
      my_Mailer("Its out of stock")

THE JSON FORMAT IS:


{
  "request_info": {},
  "request_metadata": {},
  "request_parameters": {},
  "product": {
    "brand": "XXX",
    "title": "MyProductName",
    "upc": "XXXXXXXX",
    "item_id": "XXXXXXXXXX",
    "product_id": "XXXXXXXXXX",
    "item_number": "XXXXXXXXX",
    
    "model": "85888",
    "ratings_total": 45,
    "rating": 4.6,
    "type": "Housewares",
    "buybox_winner": {
      "price": 100.0,
      "was_price": 199.95,
      "currency_symbol": "$",
      "id": "XXXXXXXXXXXXXXXXXXXXXXXX",
      "": {
        "name": "MyStore.com",
        "id_secondary": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
        "id": "XXXXXXXXXXXXXXXXXXXXXXXXXX"
      },
      "availability": {
        "raw": "OUT_OF_STOCK",
        "in_stock": false,
        "preorder": false
      }
}

My error is KeyError: 'product.buybox_winner.seller.name'

I can't seem to parse this JSON file because I don't know how to reference the key correctly in order to check it.

NeilS
  • 65
  • 1
  • 7
  • Does this answer your question? [How to access an element inside a nested dictionary in python?](https://stackoverflow.com/questions/62733489/how-to-access-an-element-inside-a-nested-dictionary-in-python) – mkrieger1 Nov 26 '21 at 14:42
  • `"product.buybox_winner.seller.name"` this key doesn't exist in the dictionary. It seems you're trying to access a path in the json. Dictionaries don't support that. You'll need to do `product_data['product']['buybox_winner']['seller']['name']` – rdas Nov 26 '21 at 14:42

1 Answers1

0

Try the following:

product_data['product']['buybox_winner']['seller']['name']

You need to access JSON using key values in that manner.