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
1 |
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
1 |
import json |
if json object is :
1 2 3 4 5 |
{ "id": 5331, "firstName": "max", "lastName": "well" } |
below is the code of for loading json object from string
1 |
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
1 2 3 4 5 |
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 .
1 2 3 4 5 6 7 |
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