Filter Data Using JSON Queries
In this step, you'll use the custom json_extract
function to filter data based on values within the JSON fields.
Open the json_extractor.py
file again.
nano json_extractor.py
Modify the json_extractor.py
file to include a function for querying the database:
import sqlite3
import json
def json_extract(json_str, path):
try:
json_data = json.loads(json_str)
path_components = path.split('.')
value = json_data
for component in path_components:
value = value.get(component)
return value
except (json.JSONDecodeError, AttributeError, TypeError):
return None
def connect_db(db_path):
conn = sqlite3.connect(db_path)
conn.create_function("json_extract", 2, json_extract)
return conn
def filter_products(db_path, json_path, value):
conn = connect_db(db_path)
cursor = conn.cursor()
query = f"SELECT * FROM products WHERE json_extract(details, '{json_path}') = '{value}'"
cursor.execute(query)
results = cursor.fetchall()
conn.close()
return results
if __name__ == '__main__':
## Example usage:
dell_products = filter_products('mydatabase.db', 'brand', 'Dell')
print("Products with brand 'Dell':", dell_products)
intel_products = filter_products('mydatabase.db', 'specs.cpu', 'Intel i7')
print("Products with CPU 'Intel i7':", intel_products)
This code adds a filter_products
function that takes a database path, a JSON path, and a value as input. It then connects to the database, registers the json_extract
function, and executes a query to find all products where the value at the specified JSON path matches the given value.
Save the file and exit nano
.
Now, run the Python script.
python3 json_extractor.py
Expected Output:
Products with brand 'Dell': [(1, 'Laptop', '{"brand": "Dell", "model": "XPS 13", "specs": {"cpu": "Intel i7", "memory": "16GB", "storage": "512GB SSD"}}')]
Products with CPU 'Intel i7': [(1, 'Laptop', '{"brand": "Dell", "model": "XPS 13", "specs": {"cpu": "Intel i7", "memory": "16GB", "storage": "512GB SSD"}}')]
This output shows the products that match the specified criteria.