if json key exists python code example
Example 1: python json check if key exists
import json
if 'id' not in dest:
dest['id'] = -1
targetId = dest['id']
Example 2: python check if key exist in json
json_string = """{"a": 1, "b": 2, "c": 3}"""
a_dictionary = json.loads(json_string)
b_in_dict = "b" in a_dictionary
Example 3: how to check if json has a key python
b_in_dict = "b" in a_dictionary
Example 4: python json check if key exist
if 'to' not in data:
raise ValueError("No target in given data")
if 'data' not in data['to']:
raise ValueError("No data for target")