Package: pyjq
pyjq: Binding for jq JSON Processorpyjq 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/jqis 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"),...)>>>importpyjq>>>pyjq.first('.parameters[] | {"param_name": .name, "param_type":.type}',data){'param_name':'PKG_TAG_NAME','param_type':None}InstallYou 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.pipinstallpyjqAPIFor jq script,see its manual.Only four APIs are provided:allfirstonecompilealltransforms 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'}]alltakes an optional argumentvars.varsis a dictonary of predefined variables forscript.
The values invarsare available in thescriptas a$key.
That is,varsworks like--argoption and--argjsonoption of jq command.>>>pyjq.all('{user, title: .titles[]} | select(.title == $title)',value,vars={"title":"More JQ"})[{'user':'stedolan','title':'More JQ'}]alltakes an optional argumenturl.
Ifurlis given, the subject of transformation is retrieved from theurl.>>pyjq.all(".[] | .login",url="https://api.github.com/repos/stedolan/jq/contributors")# get all contributors of jq['nicowilliams','stedolan','dtolnay',...]Additionally,alltakes an optional argumentopener.
The defaultopenerwill download contents usingurllib.request.urlopenand decode usingjson.decode.
However, you can customize this behavior using a customopener.firstandoneare similar to toall.firstreturns the first result of transformation.
When there are no results,firstreturnsNoneor the givendefault.>>>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'onereturns 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(mostrecentcalllast):IndexError:Resultofjqisempty>>>pyjq.one('.titles[] | select(test("J"))',data)Traceback(mostrecentcalllast):IndexError:Resultofjqhavemultipleelementscompileis similar tore.compile. It accepts jq script and returns an object with methods.>>>data={"user":"stedolan","titles":["JQ Primer","More JQ"]}>>>importpyjq>>>pat=pyjq.compile('{user, title: .titles[]}')>>>pat.all(data)[{'user':'stedolan','title':'JQ Primer'},{'user':'stedolan','title':'More JQ'}]Limitationsjq is a JSON Processor. Therefore pyjq is able to process only
"JSON compatible" data (object made only from str, int, float, list, dict).Q&AHow can I process a json string (f.e. gotten from an API) with pyjq?You should calljson.loadsfrom the standard library on the string, before you pass it to pyjq.AuthorOMOTO KenjiLicenseMIT License. SeeLICENSE.This package includesjqandoniguruma. Their license files are included in their respective archive files.jq:dependencies/jq-1.5.tar.gzoniguruma:dependencies/onig-6.9.0.tar.gzChangelog2.6.0Supports 3.102.5.1Fixed typo.2.5.0Supports only 3.7+.Added pyjq.ScriptRuntimeError.2.4.0Dropped support for Python 2.7, 3.4, 3.5 (Supports only 3.6+).2.3.0Supported WindowsPE(msys)2.2.0Addedlibrary_paths=option.2.1.0API's translate JS object not todictbut tocollections.OrderedDict.2.0.0Semantic versioning.Bundle source codes of jq and oniguruma.Supported Python 3.5.Dropped support for Python 3.2.Aededallmethod.1.0First release.