Skip to main content

Binding for jq JSON processor.

Project description

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.

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'}

API

For jq script, see its manual.

Only four APIs are provided. They are apply, one, first, compile.

apply(script, value, **kw)

Transform object by jq script, returning all results as list.

first(script, value, default=None, **kw)

Transform object by jq script, returning the first result. Return default if result is empty.

one(script, value, **kw)

Transform object by jq script, returning the first result. Raise ValueError unless results does not include exactly one element.

compile(script, **kw)

Compile a jq script, retuning a script object.

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).

Install

As usual:

pip install pyjq

For your information, https://pypi.python.org/pypi/jq is a also jq bindings but different and incompatible with pyjq.

Build from source code

Build jq

You have to build and install jq library before you build this module.

jq requires following packages:

  • Flex

  • Bison

  • GCC

  • Make

  • Autotools

These will be installed by your system’s package manager (apt, yum, or else).

Checkout source code from github:

git clone https://github.com/stedolan/jq
cd jq
git checkout jq-1.3

Where, you can checkout master branch or newer release than jq-1.3. However, author of pyjq have not build with any release other than jq-1.3.

Build and install jq:

autoreconf -i
./configure
make
sudo make install

Some system have cached list of dynamic link libraries. In such system, library of jq is not loaded and importing python binding fails.

Command to refresh the cache is system specific. In the case Linux Mint 14:

ldconfig

Install pyjq

setuptools and cython are required to build and to install this module.

You can build and install this module by usual way.

python setup.py install

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.

License

Copyright (c) 2014 OMOTO Kenji. Released under the MIT license. See LICENSE for details.

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-1.0.tar.gz (60.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pyjq-1.0.tar.gz
  • Upload date:
  • Size: 60.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyjq-1.0.tar.gz
Algorithm Hash digest
SHA256 fc06f90a351ebb05e7ab6c12bb769d7771a0d76df6eb07ea00de137e67c348b5
MD5 9fa52399691d3989f262d36c4e5b66da
BLAKE2b-256 14c803bc4f41abe4b1e900d9bc1f1cebdb79f998a5563364e6ea1533f7b228f8

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