Skip to main content

Define and run test queries from JSON.

Project description

JqSON

The JqSON library was developed to provide a simple way to define queries in JSON format, which can later be run in Python.

from dataclasses import dataclass
from jqson import Query

@dataclass
class Person(object):
    name: str = ""
    age: int = 0
    children: list[Person] = ()

P1 = Person(name="P1", age=10)
P2 = Person(name="P2", age=7)
P3 = Person(name="P3", age=50, children=[P1, P2])
P4 = Person(name="P4", age=77, children=[P3])

text = """
[
    {"query": "where", "key": [
        {"query": "not_empty", "left": "children"}
    ]},
    {"query": "all", "key": [
        {"query": "attr", "name":"age"},
        {"query": "and", "conditions": [
            {"query": "not_null"},
            {"query": ">", "value": 20},
            {"query": "<", "value": 100}
        ]}
    ]}
]
"""

data = [P1, P2, P3, P4]
query = Query.from_text(text)
print(query(data))

# equivalent of
print(all((p.age is not None and 20 < p.age < 100) for p in data if p.children))

Installation

The JqSON library is fully implemented in Python. No additional compiler is necessary. After downloading the source code just run the following command from the jqson folder:

$ python setup.py install

or simply by using pip

$ pip install jqson

Requirements

Selector Queries

  • Path: {"query": "path", "path": "PATH"}
  • Attr: {"query": "attr", "name": "NAME"}
  • Item: {"query": "item", "key": "KEY"}
  • Bool: {"query": "bool"}
  • Len: {"query": "len"}
  • Variable: {"query": "var", "name": "NAME", "value": "VALUE", "path": "PATH"}

Collections Queries

  • Select: {"query": "select", "path": "PATH"}

  • Many: {"query": "many", "path": "PATH"}

  • Sum: {"query": "sum", "path": "PATH"}

  • Mean: {"query": "mean", "path": "PATH"}

  • Where: {"query": "where", "key": "KEY"}

  • First: {"query": "first", "key": "KEY"}

  • Last: {"query": "last", "key": "KEY"}

  • Single: {"query": "single", "key": "KEY"}

  • Distinct: {"query": "distinct", "key": "KEY"}

  • Any: {"query": "any", "key": "KEY"}

  • All: {"query": "all", "key": "KEY"}

  • Noone: {"query": "none", "key": "KEY"}

  • Count: {"query": "count", "key": "KEY"}

  • Min: {"query": "min", "key": "KEY"}

  • Max: {"query": "max", "key": "KEY"}

  • Sort: {"query": "sort", "key": "KEY", "reverse": "REVERSE"}

Slicing Queries

  • Take: {"query": "take", "count": "COUNT"}
  • Skip: {"query": "skip", "count": "COUNT"}
  • Slice: {"query": "slice", "start": "START", "end": "END", "step": "STEP"}

Conditions Queries

  • AND: {"query": "and", "conditions": "CONDITIONS"}

  • OR: {"query": "or", "conditions": "CONDITIONS"}

  • ==: {"query": "==", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • !=: {"query": "!=", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • >: {"query": ">", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • <: {"query": "<", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • >=: {"query": ">=", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • <=: {"query": "<=", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • in: {"query": "in", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • contains: {"query": "contains", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • has: {"query": "has", "value": "VALUE", "left": "LEFT", "right": "RIGHT"}

  • true: {"query": "true"}

  • false: {"query": "false"}

  • null: {"query": "null"}

  • empty: {"query": "empty"}

Disclaimer

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

jqson-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

jqson-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file jqson-0.1.0.tar.gz.

File metadata

  • Download URL: jqson-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for jqson-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a892e7c91de44e97b98fa5c2486ff396b635d0aa0adbedcc8873754ab7867982
MD5 5e7525599f4fe99fc64c4e47bb13cb30
BLAKE2b-256 4d0bb88ae284d8f58fb10ca4479e7c79ee9330a30b5b1a34cd4a4733b9e5c5bc

See more details on using hashes here.

File details

Details for the file jqson-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jqson-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for jqson-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b903a3c1f9002cd7f4fcf2f384c3b600023e24eb127289d49f45ebe072006fb
MD5 17895ba2545db9f23e74446b4e4f5f2f
BLAKE2b-256 52cad7c96beb14d4122645f4541c7cb9f63c55e077ef0721d6c28103fb26cc5b

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