How to Find a Specific Field Value from a JSON list in Python?
The following example code shows how to find/search a specific field value from a JSON list in Python:
def find_price_from_json(json_list, laptop_name):
for dict in json_list:
if dict['laptop_name'] == laptop_name:
return dict['price']
#Calling method
json_list = [{"laptop_name":"dell","price":500},{"laptop_name":"acer","price":400},{"laptop_name":"samsung","price":600}]
price = find_price_from_json(json_list,'samsung')
print(price)
Using Python List comprehension to get the price of a specific laptop name:
def find_price(json_list, laptop_name):
return [p for p in json_list if p['laptop_name']==laptop_name][0]['price']
#Calling method
json_list = [{"laptop_name":"dell","price":500},{"laptop_name":"acer","price":400},{"laptop_name":"samsung","price":600}]
price = find_price(json_list,"dell")
print(price)