Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects
Before starting the Json example make sure Json is installed in Python. if it not existed you can install it using below code
pip install json
After installing the Json ModuleĀ we can use the json in our python script
we need to import json into code by using below code
import json
if json object is :
{ "id": 5331, "firstName": "max", "lastName": "well" }below is the code of for loading json object from string
jsonobject = json.loads('{"id": 5331,"firstName": "max","lastName": "well"}')now I want to check the firstName exists in my json object or not. below code is used for checking key in json object
if 'firstName' in jsonobject: print("key exists") # some stuff you can do in if condtion else: print("given key not found in json object")for complete example is below .
import json jsonObject = json.loads('{"id": 5331,"firstName": "max","lastName": "well"}') if 'firstName' in jsonObject: print("Key found in given Json Ojbect") else: print("Key not found")After CompilingĀ the code below is the output
How to Check Key Exists in Json Object in Python