jsonquery
Basic json -> sqlalchemy query builder
Installation
pip install jsonquery
Basic Usage
Let’s define a model and get an engine set up:
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
age = Column(Integer)
height = Column(Integer)
engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
model = User
session = sessionmaker(bind=engine)()
We want to get all users whose name starts with ‘Pat’ and are at least 21:
from jsonquery import jsonquery
json = {
"operator": "and",
"value": [
{
"operator": ">=",
"column": "age",
"value": 21
},
{
"operator": "ilike",
"column": "name",
"value": "pat%"
}
]
}
query = jsonquery(session, User, json)
users = query.all()
Supported Data Types
jsonquery doesn’t care about column type. Instead, it uses a whitelist of operators, where keys are strings (the same that would be passed in the “operator” field of a node) and the values are functions that take a column object and a value and return a sqlalchemy criterion. Here are some examples:
def greater_than(column, value):
return column > value
register_operator(">", greater_than)
def like(column, value):
like_func = getattr(column, 'like')
return like_func(value)
register_operator("like", like)
By default, the following are registered:
>, >=, ==, !=, <=, < like, ilike, in_
Use unregister_operator(opstring) to remove an operator.
Future Goals
There are a few features I want to add, but these are mostly convenience and aren’t necessary to the core application, which I believe is satisfied.
Compressed and/or format
Reduce repetitive column and operator specification when possible by allowing non-scalar values for column operators. By flipping the nesting restriction on logical operators, we can omit fields specified at the column level. This is especially prominent in string matching, when the column and operator are the same, but we want to compare against 3+ values.
Currently:
{
"operator": "or",
"value": [
{
"column": "age",
"operator": "<=",
"value": 16
},
{
"column": "age",
"operator": ">=",
"value": 21
},
{
"column": "age",
"operator": "==",
"value": 18
}
]
}
With compressed logical operators:
{
"column": "age"
"value": {
"operator": "or",
"value": [
{
"operator": "<=",
"value": 16
},
{
"operator": ">=",
"value": 21
},
{
"operator": "==",
"value": 18
}
]
}
}
Or, when the operator is the same:
{
"column": "name"
"operator": "like"
"value": {
"operator": "or",
"value": [
"Bill",
"Mary",
"Steve"
]
}
}
Contributors
duesenfranz - Python 3 compatibility
svisser - Python 3 compatibility
1.0.0
First public release
Metadata
Release files for jsonquery 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonquery-1.0.2.tar.gz | 5.4 kB | Without provenance | Details for jsonquery-1.0.2.tar.gz |
Release files / jsonquery-1.0.2.tar.gz
| Download URL | jsonquery-1.0.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
119717dcd14d3720c8378b32afd77d78ad423c6e80ae70bd7e8b85b2b863ffb7
|
|
BLAKE2b-256 checksum How to use checksums |
f84804c0806cce45c738cca20876fa733ca96b6179a9e5453c44f90836e72f9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for jsonquery 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.