-1

My python rest API receives a JSON and extracts the values from keys.

Problem: The program throws a keyError and exits if any one key is not present or if the keys' value is null. The function is not inside a loop.

I want the program to continue execution even if the key is not present.

Syam Mohan
  • 1
  • 1
  • 2

1 Answers1

0

The simplest way in Python is:

try:
    extract = json_resp[key]
except KeyError:
    pass

Alternatively you can use get, with a default:

extract = json_resp.get(key, default)

Which fall back to the default if the key isn't present in the dictionary.

Cole Howard
  • 343
  • 1
  • 8