### Install sqlite-utils-jq Plugin Source: https://github.com/simonw/sqlite-utils-jq/blob/main/README.md Installs the sqlite-utils-jq plugin using the sqlite-utils command-line tool. This command should be run in the same environment where sqlite-utils is installed. ```bash sqlite-utils install sqlite-utils-jq ``` -------------------------------- ### Set up sqlite-utils-jq for Local Development Source: https://github.com/simonw/sqlite-utils-jq/blob/main/README.md Provides instructions for setting up the sqlite-utils-jq plugin locally for development. This involves checking out the code, creating a virtual environment, and installing dependencies including test dependencies. ```bash cd sqlite-utils-jq python3 -m venv venv source venv/bin/activate pip install -e '.[test]' ``` -------------------------------- ### jq() SQL Function Examples Source: https://context7.com/simonw/sqlite-utils-jq/llms.txt Demonstrates the usage of the `jq()` SQL function within sqlite-utils for various JSON operations, including basic value extraction, nested value retrieval, array filtering, and querying JSON columns in database tables. It shows how to pass JSON data and jq expressions as parameters. ```bash # Basic value extraction from JSON object sqlite-utils memory "select jq(:doc, :expr) as result" \ -p doc '{"foo": "bar"}' \ -p expr '.foo' \ --table # Output: # result # -------- # "bar" # Extract nested values sqlite-utils memory "select jq(:doc, '.user.name') as name" \ -p doc '{"user": {"name": "Alice", "age": 30}}' \ --table # Output: # name #Просеך # "Alice" # Filter array elements sqlite-utils memory "select jq(:doc, '.items[] | select(.price > 10)') as expensive" \ -p doc '{"items": [{"name": "apple", "price": 5}, {"name": "laptop", "price": 999}]}' \ --table # Output: # expensive # --------------------------- # {"name":"laptop","price":999} # Use with actual database tables containing JSON columns sqlite-utils memory " create table events (id integer primary key, data text); insert into events (data) values ('{"type": "click", "x": 100, "y": 200}'); insert into events (data) values ('{"type": "scroll", "distance": 50}'); select id, jq(data, '.type') as event_type from events; " --table # Output: # id event_type # ---- ---------- # 1 "click" # 2 "scroll" ``` -------------------------------- ### Python API Usage of jq() SQL Function Source: https://context7.com/simonw/sqlite-utils-jq/llms.txt Illustrates how to use the `jq()` SQL function when sqlite-utils is employed as a Python library. It covers basic queries, extracting nested JSON data, querying JSON fields from database tables, and demonstrates the error handling mechanism where invalid jq expressions return an error object. ```python import sqlite_utils # Create an in-memory database (plugin auto-registers the jq function) db = sqlite_utils.Database(memory=True) # Basic jq query result = list(db.query("select jq(?, ?) as result", ('{"foo": "bar"}', ".foo"))) print(result) # Output: [{'result': '"bar"'}] # Extract from nested JSON result = list(db.query( "select jq(?, ?) as value", ('{"data": {"items": [1, 2, 3]}}', '.data.items[1]') )) print(result) # Output: [{'result': '2'}] # Working with tables containing JSON data db.execute("create table configs (name text, settings text)") db["configs"].insert_all([ {"name": "app1", "settings": '{"debug": true, "port": 8080}'}, {"name": "app2", "settings": '{"debug": false, "port": 3000}'} ]) # Query JSON fields from table results = list(db.query("select name, jq(settings, '.port') as port from configs")) print(results) # Output: [{'name': 'app1', 'port': '8080'}, {'name': 'app2', 'port': '3000'}] # Error handling - invalid jq expression returns error object result = list(db.query("select jq(?, ?) as result", ('{"foo": "bar"}', "invalid["))) print(result) # Output: [{'result': '{"error": "..."}'}] ``` -------------------------------- ### Use jq() SQL Function with JSON Data Source: https://github.com/simonw/sqlite-utils-jq/blob/main/README.md Demonstrates how to use the jq() SQL function provided by the sqlite-utils-jq plugin. It selects a JSON field using a jq expression. The function takes the JSON document and the jq expression as parameters. ```bash sqlite-utils memory "select jq(:doc, :expr) as result" \ -p doc '{"foo": "bar"}' \ -p expr '.foo' \ --table ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.