Skip to main content

Easy filtering based on jq syntax

Project description

Filters

Build Status

jqfilters allows to easily create filter objects, which can be applied to JSON-like dictionaries. It is based on the popular JSON parsing tool jq, so the syntax mimics jq's.

For showing both, the same JSON object will be used:

>>> person = {
...     "name": "Alice"
...     "books": [
...         {"name": "The name of the wind"},
...         {"name": "A feast for crows"},
...         {"name": "Continuous delivery"}
...     ],
...     "age": 24,
...     "location": "Lancashire"
... }

Simple Queries

We will create a filter that will return True for people living in Yorkshire.

>>> from jqfilters import Filter
>>> is_from_yorkshire = Filter(op1='.location', operator='eq', op2='Yorshire')
>>> is_from_yorkshire(person)
False

We can also apply some transformation to operands (see jqfilters.operations):

>>> from jqfilters import Filter
>>> reads_a_lot = Filter(op1='.books', transform1='len', operator='ge', op2=3)
>>> reads_a_lot(person)
True

Complex Queries

We will create a filter that will return True when any of the book name is "A game of thrones" or when the person is older than 18. As this specification is more complex, the :meth:fromConfig <jqfilters.filters.Filter.fromConfig> method will be used.

>>> specs = {
...     "op1": {
...         "op1": ".books[].name",
...         "operator": "contains",
...         "op2": "A game of thrones"
...     },
...     "operator": "or",
...     "op2": {
...         "op1": ".age",
...         "operator": "ge",
...         "op2": 18
...     }
... }
>>> adult_got_fans = Filter.fromConfig(specs)
>>> adult_got_fans(person)
True

A filter can be inspected in an easier way by just prompting it:

>>> adult_got_fans
>>> ((.books[].name - contains - A game of thrones) - or - (.age - ge - 18))

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

jqfilters-0.9.4.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file jqfilters-0.9.4.tar.gz.

File metadata

  • Download URL: jqfilters-0.9.4.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0

File hashes

Hashes for jqfilters-0.9.4.tar.gz
Algorithm Hash digest
SHA256 cca25b823de0342561063d37cdca87ef70677cb5bc2482866572fb854fd2e87f
MD5 4f0e0a7c811192c81dabf4f08e9b955a
BLAKE2b-256 09bea4391215f60efc5bc2692d7da4cc2d69a1d3182686ceca32b188d4e7afe6

See more details on using hashes here.

Supported by

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