How to search for JSON data using python – Linux hint

How to search for JSON data using python – Linux hint

One of the most commonly used methods for serializing data is the JSON format. Python has a built-in JSON module for working with JSON data. It supports all kinds of primitive data types such as numbers, strings, etc. and Python objects. The data is stored in a structured format in JSON. Sometimes you need to search for specific data from a large JSON string or a JSON file. There are many ways to find specific data from JSON data. This article shows you how to use a Python script to search for JSON data by key or value.

Example 1: Simple JSON Data Search Keys

The following script shows you how to search whether or not a certain key exists in the JSON string. It defines the variable customerData for the storage of JSON data. The value of the key is interpreted as the login of the user. The JSON Module load() method is used to load JSON data into a variable named by the customer. Then the operator in is used to find the key.

#!/usr/bin/env python3

# import json module

# Define Json
Customer Data ={
id: 3425678,
name : John Michael, e-mail
: [email protected], type
: regular, address
: 4258 Popular Chase Lane, Boise, Idaho.
}

# Enter the value of the key you want to find
keyVal = input (Enter the key value: n)

# download json data
client = json.load (customerData)
# find the value of the key with the operator in
as keyVal in client :
#
print (%s is in JSON %keyVal data)
print (value, keyVal,is, client [keyVal])
else :
# Print message if value not present
printout (%s not found in JSON %keyFall data)

A way out:

Here the scenario is executed twice. The existing key value is specified the first time, while the non-existent key value is specified the second time.

How to search for JSON data using python – Linux hint

Example 2: Searching for a specific value in JSONdata

The following script shows you how to search for a specific value in the JSON data. The candidate variable contains JSON data, where the key is used to store the candidate’s name, and the value to store the candidate’s name is present or absent. The script searches for the value Absent in the JSON data and returns the value of the corresponding name. JSON data iteration is used here for the loop.

#!/usr/bin/env python3

# Import Json module

# Determine dates Json
candidates = [
Scott S. Aldridge ] Present,
Joe L. Foss: Right now, Clyde M Gold: Present, Monique C. Doolittle: Negative, David M. Volkert: Currently
Israel M Oneal: Present,
Elizabeth M. Groff: No, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no.
}
# Initialize counter
Counter = 0
# json data
appList = json.load(credentials)
# iterate json to find the list of missing
applications for the key in appList:
if (appList [key] == ‘Absent’):
# Check the print counter when (counter == 0):- Print counter (key) = counter + 1

# Print the message if no caller is missing
as (counter == 0) : Print
(All applicants present)

A way out:

According to JSON’s scenario, there are no two candidates. This is the result obtained after the execution of the script:

How to search for JSON data using python – Linux hint

Example 3: Searching for values in the JSON table data using user function

The following script defines a JSON array called jsondata. Here the specific value of the key is searched for and if the value exists, the value of another related key is produced. Here we define the function search_price(), which takes the value of the name key to be searched into the JSON data and returns the value of the corresponding unit_price parameter.

#!/usr/bin/env python3

# import json module

# Define the variable Json
jsondata = [
{
name:pen,
unit_price:5
},
{
name:eraser,
unit price3
},
{
name:pencil,
unit price:10
},
{
name:white paper,
unit price:15
}
}.

# download json data
elements = json.load(jsondata)

# Enter the name of the element to search for element
= input (Enter the name of the element:n)

# Define a search function
def search_price (name):
for the key value in the elements:
if name.lower() == keyval[‘name’].lower():
return keyval[‘unit_price’].

# Check the return value and print the message
if (search_price(item) != None) :
Print (price : search price(item))
other :
-Print (item not found)

A way out:

In this output, the script is executed twice. The value of the name key that exists in the JSON data is taken as a pencil. The unit of measurement for the pencil is 10, which is printed. Then the input value is a book that is not in the JSON data.

How to search for JSON data using python – Linux hint

Example 4: Search button for nested JSON data

The following script shows the steps for finding the value of a particular key in the nested JSON data. To save the attached data, an attached JSON variable called nestedData is declared here. In this scenario we are looking for a brand of women’s watches.

#!/usr/bin/env python3

# import json module

# Identify the json variable from the nested dataData = {clock:{Men:{brand:Titan,Price:200},Women:{brand:Citizen,Price:250},Child:{brand:Blancpain,Price:100}}.

# Download Json data
Watchlist = json.load (nestedData)

# Find a brand for women
if the brand is on the watchlist [watch] [woman] :
print (watchlist [watch] [woman] [brand])

A way out:

In the above scenario, there is only one brand value for women’s watches, namely a Citizen. After executing the script, the following result is displayed.

How to search for JSON data using python – Linux hint

Example 5: Searching for a dataset from a JSON file with filter method and lambda

The following steps show you how to search for an item in a JSON file using a specific key and values. The content of the book.json file is presented below.

The books. John

{
isbn: 7799349885, name
: Fundamentals of Vehicle Dynamics, author: Joop P. Peacock Loops
},
{
isbn : 7799349885, name
: Power and combustion in jet engines, author : C. Arkumanis and T. Kamimoto
},
{
isbn : 7799349885, name
: Auto-ergonomy: driver interaction, author : [ Nikolaos Gkikas
}
]

The following script searches for the book.json record, encoding the author value of Nikolaos Gkikas using the lambing method and filter().

#!/usr/bin/env python3

# Import JSON module
import json

# Open an existing JSON file to load into variable
using open (‘books.json’) as jsondata:
data = json.load(jsondata)

# Find data by key and value using the filter method and
-Print(list(filter(lambda x:x[author]==Nikolaos Gkikas,data)))

A way out:

After executing the script, the following output appears.

How to search for JSON data using python – Linux hint

Conclusion:

When we are working with a large amount of JSON data and need to easily learn specific information from that data, we need to use effective tools to perform this task. This article explains the different ways to find the key and value of JSON data to help Python users with this process.parsing json from command line using python,python json,python extract data from json file,extract fields from json python,json to list python,convert json to string python,python create json array,how to read json file

More Stories
How to upgrade to Ubuntu 20.04 From Ubuntu 18.04 to Ubuntu 19.10 Right now