How to print particular JSON value in Python

Hi, everyone in this post we will learn how we can print particular JSON value in Python.

what is the JSON file?

JSON stands for JavaScript Object Notation and it is used to store data in an organized manner.

To import JSON files in Python we use:-

data=json.load("Name of the file")

So after importing the data, the question arises that how we can access the data inside the JSON file.

So we only have to use the slicing method which we often use in strings or lists.

Now take a look at the code to access a particular data in the JSON file.

import json

fp=open("example_2.json")
data=json.load(fp)
print("Here is the data whic we have imported\n",data)
#  Use the slicing to get a particular value from a json file.
print("First Print")
print(data['quiz']['sport'])
print("Second Print")
print(data['quiz']['sport']['q1']['options'])

Here is the output:-

Print particular JSON value in Python

In our code, we have first imported the json Python module. After that, we open our JSON file. Next, we use the json.load() to load JSON data from our JSON file. After that, we are able to access the particular JSON values using the keys.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *