3

So I am using a for loop to determine if a specific key exists in a array. But so far every method I tried didn't work for what I am trying to accomplish.

My code looks as follow:

for i in range(1,len(opslag)):
     info_product = 'https://examplesite.com/'+str(opslag[i])+'.json'
     info_get = session.get(info_product)
     text_info_prdt = info_get.text
     json_info_prdt =json.loads(text_info_prdt)
     if json_info_prdt['product']['metafields']['meta_title_nl'] in json_info_prdt['product']['metafields']:
        print(json_info_prdt['product']['metafields']['meta_title_nl'])
     else:
         print(json_info_prdt['product']['id'])

So the value in this case json_info_prdt['product']['metafields']['meta_title_nl'] , doesn't exist in every array I am trying to loop through. So sometimes the if else statement will work, and the value will be printed. But sometimes the value json_info_prdt['product']['metafields']['meta_title_nl'] doesn't exist at all and gives me a KeyError.

So What I want to do is to get all the ID's of the products that return a KeyError but I don't want the loop to stop but to continue till it's finished. So I also don't want the loop to restart itself because that's pointless.

My JSON looks as follow:

{
   product:{
      article_code:"",
      barcode:"",
      brand_id:null,
      created_at:"2017-07-07T12:49:23+02:00",
      data01:"",
      data02:"",
      data03:"",
      delivery_date_id:null,
      has_custom_fields:true,
      has_discounts:false,
      has_matrix:false,
      hits:0,
      hs_code:null,
      id:52847777,
      image_id:130661048,
      is_visible:true,
      price_excl:0,
      price_incl:0,
      price_old_excl:0,
      price_old_incl:0,
      product_set_id:383078,
      product_type_id:null,
      search_context:"",
      shop_id:240359,
      sku:"",
      supplier_id:null,
      updated_at:"2018-07-10T10:53:15+02:00",
      variants_count:4,
      visibility:"visible",
      weight:0,
      custom_fields:[

      ],
      variants:[

      ],
      product_relations:[

      ],
      product_categories:[

      ],
      product_discounts:[

      ],
      product_type:null,
      product_filter_values:[

      ],
      product_bundles:[

      ],
      metafields:{
         meta_title_nl:"Big House",
         meta_title_en:"cxzcxzcxz",
         meta_description_nl:"This is a big house"
      },
      supplier:null,
      product_images:[

      ],
      brand:null,
      delivery_date:null,
      image:{

      },
      nl:{

      },
      en:{

      },
      tags:null
   }
}

As you can see the value meta_title_nl exists in this one, but sometimes it doesn't exist.

2 Answers 2

2

The if-else-structure for catching errors is called try-except. So you should use :

try:
    print(json_info_prdt['product']['metafields']['meta_title_nl'])
except:
    print(json_info_prdt['product']['id'])
Sign up to request clarification or add additional context in comments.

1 Comment

You 're welcome. Btw in the not nested case of a dict, one would usually use the get method, which provides the opportunity to define a default value if key is missing. However, for nested dicts you might want to read this stackoverflow.com/q/25833613/8300135 before.
2

You should test if the key exists in the dict if it can sometimes be absent:

Change:

if json_info_prdt['product']['metafields']['meta_title_nl'] in json_info_prdt['product']['metafields']:

to:

if 'meta_title_nl' in json_info_prdt['product']['metafields']:

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.