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 '[1, 2]' | uv run main.py 'this[0]'  # 1

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.2.tar.gz (18.1 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.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jpq-0.1.2.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jpq-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d85b4d7726fa9e2ae7db7ec1695ae34897e4f2511f357dfd5d4bebd5164d72ff
MD5 fb5464dc52bf949816487600196dfb01
BLAKE2b-256 b9f5d672ab390b3ed5b0f716caea87a09faf9e413cc3445588eb714ddd2dbcaa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jpq-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jpq-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 437ada17b5bee267e55215976974e17f7c63dd1ad72b97ba4457a36f44e4e963
MD5 a83d6eae2c62911ff8a5bd1c4dcd73f7
BLAKE2b-256 97b5b4e364f240063b689c8522e42fe00339ade423a0d647911849c5fec3b9fa

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