Skip to main content

jq-style JSON filtering with Python expressions.

Project description

jpq

jq-style JSON filtering, but with Python expressions.

The parsed stdin JSON is bound to this; the value of the expression is printed as JSON.

Install

From PyPI:

uv tool install jpq
# or: pipx install jpq
# or: pip install --user jpq

From source, after cloning:

uv tool install .

Or run straight from the checkout without (re)installing:

echo '{"name":"alice"}' | uv run main.py 'this["name"]'

Usage

echo '{"name":"alice","age":30}' | jpq 'this["name"]'
# "alice"

echo '[1,2,3,4,5]' | jpq 'statistics.mean(this)'
# 3

echo '[{"k":"a"},{"k":"b"},{"k":"a"}]' | jpq 'collections.Counter(el["k"] for el in this)'
# {"a": 2, "b": 1}

Pre-imported in the eval namespace: re, collections, itertools, statistics, math, datetime, plus all builtins.

Run jpq --help for more.

Advanced examples

Pipe an API response through a multi-line expression to reshape it:

curl -s https://api.github.com/repos/python/cpython | jpq '
{
"stars": this["stargazers_count"],
"last_pushed_seconds_ago": (
    datetime.datetime.now(tz=datetime.UTC)
    - datetime.datetime.fromisoformat(this["pushed_at"])
).total_seconds()
}'
# {"stars": 72644, "last_pushed_seconds_ago": 2168.664175}

Read environment variables:

export PASS=secret123
echo '{"url": "postgres://user:pass@host:port/db"}' | jpq 'this["url"].replace("pass", env("PASS"))'
# "postgres://user:secret123@host:port/db"

Here env("PASS") is equivalent to os.environ["PASS"] (see helpers.py).

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

jpq-0.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

jpq-0.1.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file jpq-0.1.1.tar.gz.

File metadata

  • Download URL: jpq-0.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for jpq-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9f7c0874d3154280075f2df367fdc23e6eb11ba8e0d1fb4ddcd3a3a7e846f412
MD5 74c3c2f66ba27c7919e75cb0e2b01059
BLAKE2b-256 f5f23969c644bc421bfa79374b8f801b0390447ea3ffff001334ba9b7d6bff42

See more details on using hashes here.

File details

Details for the file jpq-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: jpq-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for jpq-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6412c053712813190dbdb8fb22e48284d68ffb907582d611708223587319628
MD5 7d09facfa8b6b1630bf4bc65f0ccf584
BLAKE2b-256 9b27353943c52733639e1231a05c6ec0a36d3182344e07e0c2ea075ee26e020c

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