Package: jq
This project contains Python bindings forjq1.7.InstallationWheels are built for various Python versions and architectures on Linux and Mac OS X.
On these platforms, you should be able to install jq with a normal pip install:pipinstalljqIf a wheel is not available,
the source for jq 1.7 is built.
This requires:AutoreconfThe normal C compiler toolchain, such as gcc and make.libtoolPython headers.Alternatively, set the environment variableJQPY_USE_SYSTEM_LIBSto1when installing the package
to use the libjq and libonig versions available on the system rather than building them.Debian, Ubuntu or relativesIf on Debian, Ubuntu or relatives, running the following command should be sufficient:apt-getinstallautoconfautomakebuild-essentiallibtoolpython-devRed Hat, Fedora, CentOS or relativesIf on Red Hat, Fedora, CentOS, or relatives, running the following command should be sufficient:yumgroupinstall"Development Tools"yuminstallautoconfautomakelibtoolpythonpython-develMac OS XIf on Mac OS X, you probably want to installXcodeandHomebrew.
Once Homebrew is installed, you can install the remaining dependencies with:brewinstallautoconfautomakelibtoolUsageUsing jq requires three steps:Calljq.compile()to compile a jq program.Call an input method on the compiled program to supply the input.Call an output method on the result to retrieve the output.For instance:importjqassertjq.compile(".+5").input_value(42).first()==47Input methodsCall.input_value()to supply a valid JSON value, such as the values returned fromjson.load:importjqassertjq.compile(".").input_value(None).first()==Noneassertjq.compile(".").input_value(42).first()==42assertjq.compile(".").input_value(0.42).first()==0.42assertjq.compile(".").input_value(True).first()==Trueassertjq.compile(".").input_value("hello").first()=="hello"Call.input_values()to supply multiple valid JSON values, such as the values returned fromjson.load:importjqassertjq.compile(".+5").input_values([1,2,3]).all()==[6,7,8]Call.input_text()to supply unparsed JSON text:importjqassertjq.compile(".").input_text("null").first()==Noneassertjq.compile(".").input_text("42").first()==42assertjq.compile(".").input_text("0.42").first()==0.42assertjq.compile(".").input_text("true").first()==Trueassertjq.compile(".").input_text('"hello"').first()=="hello"assertjq.compile(".").input_text("1\n2\n3").all()==[1,2,3]Passslurp=Trueto.input_text()to read the entire input into an array:importjqassertjq.compile(".").input_text("1\n2\n3",slurp=True).first()==[1,2,3]You can also call the olderinput()method by passing:a valid JSON value, such as the values returned fromjson.load, as a positional argumentunparsed JSON text as the keyword argumenttextFor instance:importjqassertjq.compile(".").input("hello").first()=="hello"assertjq.compile(".").input(text='"hello"').first()=="hello"Output methodsCallingfirst()on the result will run the program with the given input,
and return the first output element.importjqassertjq.compile(".").input_value("hello").first()=="hello"assertjq.compile("[.[]+1]").input_value([1,2,3]).first()==[2,3,4]assertjq.compile(".[]+1").input_value([1,2,3]).first()==2Calltext()instead offirst()to serialise the output into JSON text:assertjq.compile(".").input_value("42").text()=='"42"'When callingtext(), if there are multiple output elements, each element is represented by a separate line:assertjq.compile(".[]").input_value([1,2,3]).text()=="1\n2\n3"Callall()to get all of the output elements in a list:assertjq.compile(".[]+1").input_value([1,2,3]).all()==[2,3,4]Calliter()to get all of the output elements as an iterator:iterator=iter(jq.compile(".[]+1").input_value([1,2,3]))assertnext(iterator,None)==2assertnext(iterator,None)==3assertnext(iterator,None)==4assertnext(iterator,None)==NoneArgumentsCallingcompile()with theargsargument allows predefined variables to be used within the program:program=jq.compile("$a + $b + .",args={"a":100,"b":20})assertprogram.input_value(3).first()==123Convenience functionsConvenience functions are available to get the output for a program and input in one call:assertjq.first(".[] + 1",[1,2,3])==2assertjq.first(".[] + 1",text="[1, 2, 3]")==2assertjq.text(".[] + 1",[1,2,3])=="2\n3\n4"assertjq.all(".[] + 1",[1,2,3])==[2,3,4]assertlist(jq.iter(".[] + 1",[1,2,3]))==[2,3,4]Original program stringThe original program string is available on a compiled program as theprogram_stringattribute:program=jq.compile(".")assertprogram.program_string=="."