Skip to main content

Binding for jq JSON processor.

Project description

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 plain python data structure.

For your information, https://pypi.python.org/pypi/jq is a also jq bindings but 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_type': None, 'param_name': 'PKG_TAG_NAME'}

Install

It requires build tools such as make, automake, libtool, etc…

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 avaiable 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 got 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 simply download contents by urllib.request.urlopen and decode by json.decode. However, you can customize this behavior using custom opener.

first is almost some to all but it first returns the first result of transformation.

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

first returns default when there are no results.

>>> value = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> pyjq.first('.titles[] | select(test("e"))', value) # The first title which is contains "e"
'JQ Primer'

first returns the first result of transformation. It returns default when there are no results.

>>> value = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> pyjq.first('.titles[] | select(test("T"))', value, "Third JS") # The first title which is contains "T"
'Third JS'

one do also returns the first result of transformation but raise Exception if there are no results.

>>> value = {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
>>> pyjq.one('.titles[] | select(test("T"))', value)
IndexError: Result of jq is empty

Limitation

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 json string got from API by pyjq?

You should apply json.loads in the standard library before pass to pyjq.

Author

OMOTO Kenji

License

Released under the MIT license. See LICENSE for details.

Development

We DO commit _pyjq.c

When you edit _pyjq.pyx, you need to run cython _pyjq.pyx before to run python setup.py develop. It is because setup.py in this project does not compile .pyx to .c.

Of course, we can use Cython.Build.cythonize in setup.py to automatically compile .pyx to .c . But, it cause bootstrap problem in pip install.

So, we DO commit both of _pyjq.pyx and _pyjq.c.

Changes

2.3.0

  • Supported installing in msys.

2.2.0

  • Added library_paths argument to functions.

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.

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

pyjq-2.3.1.tar.gz (2.1 MB view details)

Uploaded Source

Built Distribution

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

pyjq-2.3.1-cp37-cp37m-macosx_10_12_x86_64.whl (571.4 kB view details)

Uploaded CPython 3.7mmacOS 10.12+ x86-64

File details

Details for the file pyjq-2.3.1.tar.gz.

File metadata

  • Download URL: pyjq-2.3.1.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for pyjq-2.3.1.tar.gz
Algorithm Hash digest
SHA256 5f3cd45b856ae4b42a1058d2e91e13d84df5ea188aedc3ae397dc0e01c270db9
MD5 71759c6e1a67a74f2df8027cb9d8861b
BLAKE2b-256 66747f5c7789c894a943088040a0c148f7ac5daddf4096cf820e3ff15ae75ea0

See more details on using hashes here.

File details

Details for the file pyjq-2.3.1-cp37-cp37m-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: pyjq-2.3.1-cp37-cp37m-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 571.4 kB
  • Tags: CPython 3.7m, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.1

File hashes

Hashes for pyjq-2.3.1-cp37-cp37m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 45f7688d8191a4043e7c4839d6cabff8f16abc98fe48c3a4a0cc2da05c62096d
MD5 cdc117d755cba30bf5a76a290ce0f5c5
BLAKE2b-256 54c480f1af2d33b787ee3c92f0083d0cc3b81fed8a5c313c1927008f50562bb3

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