0

First off I am total noob when it comes to writing python so a lot of what I've done thus far has been all learn as I go so with that said:

I have this bit of code here

if buycott_token != '':
    print("Looking up in Buycott")
    url = "https://www.buycott.com/api/v4/products/lookup"
    headers = {
    'Content-Type': 'application/json'
    }
    data={'barcode':upc,
          'access_token':buycott_token
         }
    try:
        r = requests.get(url=url, json=data, headers=headers)
        j = r.json()
        if r.status_code == 200:
        print("Buycott found it so now we're going to gather some info here and then add it to the system")
        name = j['products'][0]['product_name']
        description = j['products'][0]['product_description']
        #We now have what we need to add it to grocy so lets do that
        #Sometimes buycott returns a success but it never actually does anything so lets just make sure that we have something
        if name != '':
            add_to_system(upc, name, description)
    except requests.exceptions.Timeout:
        print("The connection timed out")
    except requests.exceptions.TooManyRedirects:
        print ("Too many redirects")
    except requests.exceptions.RequestException as e:
        print e  

98% of the time this works just fine with no issues. Then I'll scan something with my barcode scanner and I'll get

Traceback (most recent call last):
  File "./barcode_reader.py", line 231, in <module>
    increase_inventory(upc)
  File "./barcode_reader.py", line 34, in increase_inventory
    product_id_lookup(upc)
  File "./barcode_reader.py", line 79, in product_id_lookup
    upc_lookup(upc)
  File "./barcode_reader.py", line 128, in upc_lookup
    name = aj['products'][0]['product_name']
KeyError: 'products'

I am certain that it has something to do with how the json is being returned. Problem is when this is thrown it kills the script and that is that. Thank you for your assistance.

2
  • You should print the value of j. The error is telling you it doesn't have a 'products' key at the top level Commented Apr 27, 2019 at 16:47
  • You should check if 'products ' is there in the response and then fetch the name. Commented Apr 27, 2019 at 16:49

2 Answers 2

1

The problem is that there is no 'products' key in your response JSON. The workaround could be providing a default value if a 'products' key is not present:

default_value = [{'product_name': '', 'product_description': ''}]
j.get('products', default_value)[0]['product_name']

or you could simply check whether your response has the products key:

if 'products' not in j:
    return 'Product not found'
Sign up to request clarification or add additional context in comments.

1 Comment

This would be the problem essentially you are trying to look up something that doesn’t exist in the dictionary
0

I think this error is because of API doesn't give you proper json in response. So I think you can check from your side if key is in API response or not.

if 'products' in j:
   name = j['products'][0]['product_name']
   description = j['products'][0]['product_description']
else:
   #Whatever you want when 'product' is not in API response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.