Skip to main content

Convert JSON filters to Q expression

Project description

json2q

A library to convert JSON filters to Q expressions.

Usage

  • Filter fields
from tortoise.expressions import Q
from json2q import json2q

filters = {
    "name": {
        "$startsWith": "A"
    },
    "extras": {
        "age": {
            "$eq": 10
        },
    }
}

q = json2q(filters, Q)
# Q(name__startswith='A') & Q(extras__age=10)
  • Filter fields with logical operators
from tortoise.expressions import Q
from json2q import json2q

filters = {
    "$or": [
        {
            "name": {
                "$startsWith": "A"
            }
        },
        {
            "$and": [
                {
                    "age": {
                        "$gt": 10
                    },
                },
                {
                    "age": {
                        "$lt": 20
                    },
                },
            ]
        },
    ]
}

q = json2q(filters, Q)
# Q(name__startswith='A') | (Q(age__gt=10) & Q(age__lt=20))
  • Filter fields with convertion options
from tortoise.expressions import Q
from json2q import json2q

filters = {
    "extras": {
        "name": {
                "$startsWith": "A"
        },
        "age": {
            "$eq": 10
        },
    },
}

q = json2q(filters, Q, json2q.ConvertionOptions(max_depth=2))
# OK
q = json2q(filters, Q, json2q.ConvertionOptions(max_depth=1))
# raise ValueError

q = json2q(filters, Q, json2q.ConvertionOptions(max_keys=2))
# OK
q = json2q(filters, Q, json2q.ConvertionOptions(max_keys=1))
# raise ValueError

Supported Operators

Operator Description
$eq Equal
$ne Not equal
$lt Less than
$lte Less than or equal
$gt Greater than
$gte Greater than or equal
$in Included in an array
$contains Contains
$startsWith Starts with
$endsWith Ends with
$and Join the filters in "and" expression
$or Join the filters in "or" expression
$not Join the filters in "not" expression

Convertion Options

Option Default Description
max_depth 8 Max depth of nesting logical operators or fields
max_keys 64 Max keys in each level of filters

Todo

  • Support more operators
  • More filters structure validation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json2q-0.3.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json2q-0.3.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file json2q-0.3.0.tar.gz.

File metadata

  • Download URL: json2q-0.3.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.23.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for json2q-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ff598c63c1328a2703905108cf1db3f20e8d6ed83a32c6f80272161685cc8f6f
MD5 61a1330698e011248ba0c91a19e3107a
BLAKE2b-256 1bef3d138d7bcf54cf555998968b41ecf38f82c37ce69709f71be7cc9e7c4e60

See more details on using hashes here.

File details

Details for the file json2q-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: json2q-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.23.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for json2q-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 037d9a58d7fdb146263369380c5209a0563dcb93fd18dde2164be7fc3c5ecdb8
MD5 976c0ebbb9c832a64cef7be3d140d793
BLAKE2b-256 6395f32bc4ba51d8b1a63934e3db1aa3c10e42c037d5d6170878a5f8746e1b4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page