How to Check if a key exists in JSON using Python: 4 Steps

How to Check if a key exists in JSON using Python

Most of the api endpoints output the JSON resposne that can be list of arrays or list of dictionaries. JSON stands for JavaScript Object Notation. You can use these JSON responses to any applications like web,mobile e.t.c. JSON response mostly are in the dictionary form that is key-value pair. In this tutorial you will know how to check if a key exists in JSON using python through steps.

Steps to Check if a key exists in JSON using python

Lets know all the steps to check if a key exists in JSON string or not. Just follow the steps.

Step 1: Import the required library

The first step is to import all required libraries. In this example I am using the JSON module . Import it using the import statement.

import json

Step 2: Create dummy JSON string

The next step is to create a dummy JSON string. You can use the json string or parsed JSON object. Use the below line of code to create a sample JSON object.

json_data = '{"name": "Sukesh", "age": 30, "city": "Delhi"}'
data = json.loads(json_data)

Step 3: Choose the key

Now you have the JSON data ,you can choose the key to check in the JSON object. Suppose I want to check the age key then you will assign this variable to some temporary key.

check_key ="age"

Step 4: Use the in operator

In this step you will use the “in” operator on the JSON data to check the key in the data.For this you will use the for loop to iterate the data. After that you will print the output that the key you want exists in JSON data.

if key in data:
    print(f"The key '{key}' exists in the JSON data.")
else:
    print(f"The key '{key}' does not exist in the JSON data.")

You can also use the get() function to check whether key is in JSON data or not. Just pass  the key to the get() function to achieve that.

if data.get(check_key ) is not None:
    print(f"The key '{check_key }' exists in the JSON data.")
else:
    print(f"The key '{check_key }' does not exist in the JSON data.")

Full Code

import json
json_data = '{"name": "Sukesh", "age": 30, "city": "Delhi"}'
data = json.loads(json_data)
check_key ="age"
if check_key in json_data:
    print(f"The key '{check_key}' exists in the JSON data.")
else:
    print(f"The key '{check_key}' does not exist in the JSON data.")

Output

Check if a key exists in JSON using python
Check if a key exists in JSON using python

Conclusion

The response you are getting as the JSON format should be validated by checking the presence of key in it. If you are getting the JSON response that doesn’t have to key you want , you have to ignore that response. The above steps will allows you to check a key exists in JSON using python or not.

I hope you have liked this tutorial. If you have any doubt then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner