Python SDK for JSONQL — a JSON-based query language for SQL databases
Project description
jsonql-py
Python SDK for JSONQL — a JSON-based query language for SQL databases.
Installation
pip install jsonql-py
With framework extras:
pip install jsonql-py[flask] # Flask adapter
pip install jsonql-py[fastapi] # FastAPI adapter
pip install jsonql-py[django] # Django REST adapter
pip install jsonql-py[postgres] # PostgreSQL driver support
Quick Start
Query Builder
from jsonql import QueryBuilder
from jsonql.conditions import eq, gt, field, and_
query = (
QueryBuilder()
.from_table("users")
.select("id", "name", "email")
.where(and_(
field("age", gt(18)),
field("status", eq("active")),
))
.order_by("name", "-age")
.limit(10)
.build()
)
Mutation Builder
from jsonql import MutationBuilder
# Create
mutation = MutationBuilder().create({"name": "Alice", "age": 30}).build()
# Update
mutation = (
MutationBuilder()
.update({"name": "Bob"})
.where({"id": {"eq": 1}})
.build()
)
# Delete
mutation = MutationBuilder().delete().where({"id": {"eq": 1}}).build()
Transpiler
from jsonql import Parser, SQLTranspiler
parser = Parser()
query = parser.parse({
"fields": ["id", "name"],
"where": {"status": {"eq": "active"}},
"sort": ["-name"],
"limit": 10,
})
transpiler = SQLTranspiler("postgres")
result = transpiler.transpile(query, "users")
print(result.sql) # SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."status" = $1 ORDER BY "users"."name" DESC LIMIT 10
print(result.args) # ['active']
Schema Validation
from jsonql import Validator, JsonQLQuery
from jsonql.types import JsonQLSchema, JsonQLTable, JsonQLField
schema = JsonQLSchema(tables={
"users": JsonQLTable(fields={
"id": JsonQLField(type="integer"),
"name": JsonQLField(type="string"),
"secret": JsonQLField(type="string", allow_select=False),
}),
})
validator = Validator(schema, "users")
# Returns ValidationResult
result = validator.validate(JsonQLQuery(fields=["id", "name"]))
assert result.valid
# Raises JsonQLValidationError
validator.validate_or_raise(JsonQLQuery(fields=["secret"]))
Engine (Full Pipeline)
import asyncio
from jsonql import JsonQLEngine
from jsonql.types import parse_schema
schema = parse_schema({...}) # Your schema JSON
async def run_sql(sql: str, params: list) -> list[dict]:
# Your database execution logic
...
engine = (
JsonQLEngine.builder()
.postgres()
.schema(schema)
.executor(run_sql)
.debug()
.build()
)
result = asyncio.run(engine.execute({"fields": ["id", "name"]}, "users"))
print(result["data"])
Flask Adapter
from flask import Flask
from jsonql.adapters import create_flask_blueprint, AdapterOptions
app = Flask(__name__)
bp = create_flask_blueprint(AdapterOptions(
dialect="sqlite",
execute=run_sql,
schema=my_schema,
))
app.register_blueprint(bp, url_prefix="/jsonql")
FastAPI Adapter
from fastapi import FastAPI
from jsonql.adapters import create_fastapi_router, AdapterOptions
app = FastAPI()
router = create_fastapi_router(AdapterOptions(
dialect="postgres",
execute=run_sql,
schema=my_schema,
))
app.include_router(router, prefix="/jsonql")
Django Adapter
# urls.py
from django.urls import path
from jsonql.adapters import JsonQLDjangoView, AdapterOptions
options = AdapterOptions(dialect="postgres", execute=run_sql, schema=my_schema)
urlpatterns = [
path("jsonql/", JsonQLDjangoView.as_view(options=options)),
path("jsonql/<path:path>/", JsonQLDjangoView.as_view(options=options)),
]
Supported Dialects
| Dialect | Placeholder | Quoting | RETURNING |
|---|---|---|---|
postgres |
$1, $2 |
"col" |
✅ |
mysql |
?, ? |
`col` |
❌ |
sqlite |
?, ? |
"col" |
❌ |
Condition Helpers
from jsonql.conditions import (
eq, neq, gt, gte, lt, lte,
is_in, not_in, like, contains, starts_with, ends_with,
field, and_, or_, not_,
)
Error Hierarchy
JsonQLError
├── JsonQLValidationError (code: VALIDATION_ERROR)
├── JsonQLTranspileError (code: TRANSPILE_ERROR)
└── JsonQLExecutionError (code: EXECUTION_ERROR)
Development
pip install -e ".[dev]" # import name is still 'jsonql'
pytest
ruff check src/ tests/
mypy src/
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonql_py-0.1.0.tar.gz.
File metadata
- Download URL: jsonql_py-0.1.0.tar.gz
- Upload date:
- Size: 39.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f03dc9121f20b412fffc1b68f6963807edf8f94b7511a0a069f46f539279ceaa
|
|
| MD5 |
80cf5144b760c3367812245080bcbee2
|
|
| BLAKE2b-256 |
80c0f15786d0092254aa5947e6bd8414405f30201cea7d7aa4b07784cbc534ff
|
Provenance
The following attestation bundles were made for jsonql_py-0.1.0.tar.gz:
Publisher:
publish.yml on JSONQL-Standard/jsonql-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonql_py-0.1.0.tar.gz -
Subject digest:
f03dc9121f20b412fffc1b68f6963807edf8f94b7511a0a069f46f539279ceaa - Sigstore transparency entry: 1245982377
- Sigstore integration time:
-
Permalink:
JSONQL-Standard/jsonql-py@2a2f7ce9b1a65e274ce5c2bdaf47c88fac3a38db -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JSONQL-Standard
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2a2f7ce9b1a65e274ce5c2bdaf47c88fac3a38db -
Trigger Event:
release
-
Statement type:
File details
Details for the file jsonql_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jsonql_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 45.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f04495d87759f07071254d723245f74c36494f08b498843a934ea55c329e22c4
|
|
| MD5 |
9bfe9f1a7f94b8f58232831b7f7819d5
|
|
| BLAKE2b-256 |
4fe69773106efa051d44ae4a8c66a0c9b9892681d22a3289f0b018af5cd99dee
|
Provenance
The following attestation bundles were made for jsonql_py-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on JSONQL-Standard/jsonql-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonql_py-0.1.0-py3-none-any.whl -
Subject digest:
f04495d87759f07071254d723245f74c36494f08b498843a934ea55c329e22c4 - Sigstore transparency entry: 1245982380
- Sigstore integration time:
-
Permalink:
JSONQL-Standard/jsonql-py@2a2f7ce9b1a65e274ce5c2bdaf47c88fac3a38db -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JSONQL-Standard
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2a2f7ce9b1a65e274ce5c2bdaf47c88fac3a38db -
Trigger Event:
release
-
Statement type: