Skip to main content

pyjq: Binding for jq JSON Processor

CircleCI

pyjq is a Python bindings for jq (http://stedolan.github.io/jq/).

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

http://stedolan.github.io/jq/

You can seamlessly call jq script (like regular expression) and process a plain python data structure.

For your information, https://pypi.python.org/pypi/jq is another jq binding which is different and incompatible with pyjq.

Example

>>> data = dict(
...     parameters= [
...         dict(name="PKG_TAG_NAME", value="trunk"),
...         dict(name="GIT_COMMIT", value="master"),
...         dict(name="TRIGGERED_JOB", value="trunk-buildall")
...     ],
...     id="2013-12-27_00-09-37",
...     changeSet=dict(items=[], kind="git"),
... )
>>> import pyjq
>>> pyjq.first('.parameters[] | {"param_name": .name, "param_type":.type}', data)
{'param_name': 'PKG_TAG_NAME', 'param_type': None}

Install

You will need flex, bison (3.0 or newer), libtool, make, automake and autoconf to build jq. Install them by Homebrew, APT or other way.

You can install from PyPI by usual way.

pip install pyjq

API

For jq script, see its manual.

Only four APIs are provided:

  • all
  • first
  • one
  • compile

all transforms a value by JSON script and returns all results as a list.

>>> value = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> pyjq.all('{user, title: .titles[]}', value)
[{'user': 'stedolan', 'title': 'JQ Primer'}, {'user': 'stedolan', 'title': 'More JQ'}]

all takes an optional argument vars. vars is a dictonary of predefined variables for script. The values in vars are available in the script as a $key. That is, vars works like --arg option and --argjson option of jq command.

>>> pyjq.all('{user, title: .titles[]} | select(.title == $title)', value, vars={"title": "More JQ"})
[{'user': 'stedolan', 'title': 'More JQ'}]

all takes an optional argument url. If url is given, the subject of transformation is retrieved from the url.

>> pyjq.all(".[] | .login", url="https://api.github.com/repos/stedolan/jq/contributors") # get all contributors of jq
['nicowilliams', 'stedolan', 'dtolnay', ... ]

Additionally, all takes an optional argument opener. The default opener will download contents using urllib.request.urlopen and decode using json.decode. However, you can customize this behavior using a custom opener.

first and one are similar to to all.

first returns the first result of transformation. When there are no results, first returns None or the given default.

>>> data = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> pyjq.first('{user, title: .titles[]}', data)
{'user': 'stedolan', 'title': 'JQ Primer'}
>>> pyjq.first('.titles[] | select(test("T"))', data) # returns None
>>> pyjq.first('.titles[] | select(test("T"))', data, default="Third JS")
'Third JS'

one returns the only result of a transformation. It raises an exception when there are no results or when there are two or more results.

>>> data = {"user":"stedolan","titles": ["JQ Primer", "More JQ"]}
>>> pyjq.one('.titles[] | select(test("P"))', data)
'JQ Primer'
>>> pyjq.one('.titles[] | select(test("T"))', data)
Traceback (most recent call last):
IndexError: Result of jq is empty
>>> pyjq.one('.titles[] | select(test("J"))', data)
Traceback (most recent call last):
IndexError: Result of jq have multiple elements

compile is similar to re.compile. It accepts jq script and returns an object with methods.

>>> data = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> import pyjq
>>> pat = pyjq.compile('{user, title: .titles[]}')
>>> pat.all(data)
[{'user': 'stedolan', 'title': 'JQ Primer'}, {'user': 'stedolan', 'title': 'More JQ'}]

Limitations

jq is a JSON Processor. Therefore pyjq is able to process only "JSON compatible" data (object made only from str, int, float, list, dict).

Q&A

How can I process a json string (f.e. gotten from an API) with pyjq?

You should call json.loads from the standard library on the string, before you pass it to pyjq.

Author

OMOTO Kenji

License

MIT License. See LICENSE.

This package includes jq and oniguruma. Their license files are included in their respective archive files.

  • jq: dependencies/jq-1.5.tar.gz
  • oniguruma: dependencies/onig-6.9.0.tar.gz

Changelog

2.6.0

  • Supports 3.10

2.5.1

  • Fixed typo.

2.5.0

  • Supports only 3.7+.
  • Added pyjq.ScriptRuntimeError.

2.4.0

  • Dropped support for Python 2.7, 3.4, 3.5 (Supports only 3.6+).

2.3.0

  • Supported WindowsPE(msys)

2.2.0

  • Added library_paths= option.

2.1.0

  • API's translate JS object not to dict but to collections.OrderedDict.

2.0.0

  • Semantic versioning.
  • Bundle source codes of jq and oniguruma.
  • Supported Python 3.5.
  • Dropped support for Python 3.2.
  • Aeded all method.

1.0

  • First release.

Release files for pyjq 2.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyjq 2.6.0
File Size Uploaded
pyjq-2.6.0.tar.gz 2.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyjq 2.6.0
File Interpreter ABI Platform
pyjq-2.6.0-cp310-cp310-macosx_12_0_x86_64.whl CPython 3.10 CPython 3.10 macOS 12.0+ x86-64 Details

Total release size: 2.4 MB

Release files / pyjq-2.6.0.tar.gz

Download URL pyjq-2.6.0.tar.gz
Size 2.0 MB
Tags Source
SHA-256 checksum
How to use checksums
e083f326f4af8b07b8ca6424d1f99afbdd7db9b727284da5f919b9816077f2e4
BLAKE2b-256 checksum
How to use checksums
8ea7678a3f5d458c9c1002adf4938a5df830448254423095ce68d2171abf5b21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / pyjq-2.6.0-cp310-cp310-macosx_12_0_x86_64.whl

Download URL pyjq-2.6.0-cp310-cp310-macosx_12_0_x86_64.whl
Size 315.9 kB
Tags CPython 3.10 macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
6e0e4f398e81b1fb9794874d81fc9240d4a155adba5a1aecda77e717bcfae03e
BLAKE2b-256 checksum
How to use checksums
1212b1016128aed3ee6419b6aaa681e6a795747042a4b5eb24fc28bbe872484c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

2.6.0 This release

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.4.0

2 release files

2.3.1

2 release files

2.3.0

1 release file

2.2.0

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page