Skip to main content

Convert JSON filters to Q expression

Project description

json2q

A library to convert JSON filters to Q expressions.

Usage

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)
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))

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

Todo

  • Support more operators
  • More filters structure validation
  • Support config options to 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.2.0.tar.gz (5.0 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.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json2q-0.2.0.tar.gz
  • Upload date:
  • Size: 5.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 07be308464ee3abf3859394f9420f2267ef15723ef5de1af8471985a97f11054
MD5 62e712ec4436a373d133d1ea01d846b1
BLAKE2b-256 a341833362c3302b63c0411d5456c819b205c643e5dcf93857b56751d72d320d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json2q-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81e7c643fa87ff5487c2406dac080e7c9fc2ed6aa4c188240ab90d13dff87197
MD5 0ff0ab17f8f9124999b57e01e88d2ed0
BLAKE2b-256 7d24e958a1e7af2056005cf1480a7f9bd9a826ba7eee23fc77eb41326203c7c6

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