Extracting Specific Keys from JSON Lists and Downloading Images with Python
This article demonstrates how to extract specific keys from a list of JSON objects and download images to a local folder using Python, providing reusable functions, example code, and an extended version that filters out items based on a type field.
In the e‑commerce startup context, creating product assets can be costly, so the article shows how to programmatically fetch image URLs from a website and store them locally.
The first utility function extract_key_from_json_list(json_list, key) iterates over a list of JSON objects, extracts the value associated with the specified key, and returns a new list.
def extract_key_from_json_list(json_list, key):
extracted_list = []
for json_obj in json_list:
if key in json_obj:
extracted_list.append(json_obj[key])
return extracted_listThis function accepts a list of dictionaries ( json_list ) and a target key, checks each dictionary for the key, and collects the corresponding values.
An example usage demonstrates extracting the "name" field from a sample list of dictionaries:
json_list = [
{"name": "Alice", "age": 25, "city": "New York"},
{"name": "Bob", "age": 30, "city": "San Francisco"},
{"name": "Charlie", "age": 35}
]
extracted_list = extract_key_from_json_list(json_list, "name")
print(extracted_list)
# Output: ['Alice', 'Bob', 'Charlie']The second utility, download_images(image_list, output_folder) , uses the requests library to download each image URL and saves the files with numeric filenames in the specified folder.
import os
import requests
def download_images(image_list, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for index, image_url in enumerate(image_list):
try:
response = requests.get(image_url)
response.raise_for_status()
file_name = str(index) + '.jpg' # numeric index as filename
file_path = os.path.join(output_folder, file_name)
with open(file_path, 'wb') as file:
file.write(response.content)
print(f"Downloaded {file_name}")
except requests.exceptions.RequestException as e:
print(f"Error downloading image at index {index}: {str(e)}")
# Example usage
image_urls = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
]
output_folder = 'images'
download_images(image_urls, output_folder)The function creates the output directory if it does not exist, iterates over the URLs, downloads each image, and saves it with a sequential numeric name, printing a status message for each successful download.
Finally, an extended version of extract_key_from_json_list shows how to skip JSON objects where the type field equals 3 while still extracting the desired key.
def extract_key_from_json_list(json_list, key):
extracted_list = []
for json_obj in json_list:
if json_obj.get('type') != 3 and key in json_obj:
extracted_list.append(json_obj[key])
return extracted_list
json_list = [
{"type": 1, "name": "Alice", "age": 25},
{"type": 2, "name": "Bob", "age": 30},
{"type": 3, "name": "Charlie", "age": 35},
{"type": 1, "name": "Dave", "age": 40}
]
extracted_list = extract_key_from_json_list(json_list, "name")
print(extracted_list)
# Output: ['Alice', 'Bob', 'Dave']This variant filters out entries with type == 3 before collecting the specified key values, demonstrating a simple way to apply conditional logic during extraction.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.