All Projects → doloopwhile → Pyjq

doloopwhile / Pyjq

Licence: mit
A Python binding for ./jq

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Pyjq

jq-illustrated
illustrated tutorial of jq (and the scripts that create it)
Stars: ✭ 20 (-84.96%)
Mutual labels:  jq
Ticker.sh
Real-time stock tickers from the command-line.
Stars: ✭ 392 (+194.74%)
Mutual labels:  jq
Okurl
OkHttp Kotlin command line
Stars: ✭ 77 (-42.11%)
Mutual labels:  jq
dockerfiles
Dockerfiles everywhere 🐳
Stars: ✭ 17 (-87.22%)
Mutual labels:  jq
Jq.node
jq.node - like jq but WAY MORE powerful (300+ helpers 🔥 & 1.45M modules 😱)
Stars: ✭ 362 (+172.18%)
Mutual labels:  jq
Aws
A collection of bash shell scripts for automating various tasks with Amazon Web Services using the AWS CLI and jq.
Stars: ✭ 493 (+270.68%)
Mutual labels:  jq
JBOL
JBOL is a collection of modules for the JQ language.
Stars: ✭ 56 (-57.89%)
Mutual labels:  jq
Jqr
R interface to jq
Stars: ✭ 123 (-7.52%)
Mutual labels:  jq
Awesome Jq
A curated list of awesome jq tools and resources.
Stars: ✭ 391 (+193.98%)
Mutual labels:  jq
Jq Mode
Emacs major mode for editing jq queries.
Stars: ✭ 70 (-47.37%)
Mutual labels:  jq
Myflix
Myflix, a Netflix clone!
Stars: ✭ 260 (+95.49%)
Mutual labels:  jq
Jqview
simplest possible native GUI for inspecting JSON objects with jq
Stars: ✭ 355 (+166.92%)
Mutual labels:  jq
Jqaas
jq as a service
Stars: ✭ 22 (-83.46%)
Mutual labels:  jq
bro-q
Chrome Extension for JSON formatting and jq filtering in your browser.
Stars: ✭ 82 (-38.35%)
Mutual labels:  jq
Live Dl
Download live streams from YouTube
Stars: ✭ 82 (-38.35%)
Mutual labels:  jq
jq-manual-cn
jq 中文手册
Stars: ✭ 28 (-78.95%)
Mutual labels:  jq
Jqplay
A playground for jq, written in Go
Stars: ✭ 444 (+233.83%)
Mutual labels:  jq
Oq
A performant, and portable jq wrapper to facilitate the consumption and output of formats other than JSON; using jq filters to transform the data.
Stars: ✭ 132 (-0.75%)
Mutual labels:  jq
Yq
Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
Stars: ✭ 1,688 (+1169.17%)
Mutual labels:  jq
Java Jq
Lightweight Java wrapper around JQ, a flexible JSON processor available for multiple platforms
Stars: ✭ 37 (-72.18%)
Mutual labels:  jq

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 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_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

Released under the MIT license. See LICENSE for details.

Development

Pipenv

This project uses Pipenv to manage dependencies.

Please install development tools with the following command:

pipenv install --dev

Test

We can run the tests with tox.

pipenv run pytest --doctest-modules --ignore-glob='dependencies/**/*.py'

On pull request, Tox is executed in Circle CI.

We DO commit _pyjq.c

When you edit _pyjq.pyx, you need to run pipenv run cython _pyjq.pyx before you run pipenv run python setup.py develop. You need to do this 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 causes a bootstrap problem in pip install.

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

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].