Skip to main content

jq is a lightweight and flexible JSON processor.

Project description

This project contains Python bindings for jq 1.7.

Installation

Wheels 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:

pip install jq

If a wheel is not available, the source for jq 1.7 is built. This requires:

  • Autoreconf

  • The normal C compiler toolchain, such as gcc and make.

  • libtool

  • Python headers.

Alternatively, set the environment variable JQPY_USE_SYSTEM_LIBS to 1 when installing the package to use the libjq and libonig versions available on the system rather than building them.

Debian, Ubuntu or relatives

If on Debian, Ubuntu or relatives, running the following command should be sufficient:

apt-get install autoconf automake build-essential libtool python-dev

Red Hat, Fedora, CentOS or relatives

If on Red Hat, Fedora, CentOS, or relatives, running the following command should be sufficient:

yum groupinstall "Development Tools"
yum install autoconf automake libtool python python-devel

Mac OS X

If on Mac OS X, you probably want to install Xcode and Homebrew. Once Homebrew is installed, you can install the remaining dependencies with:

brew install autoconf automake libtool

Usage

Using jq requires three steps:

  1. Call jq.compile() to compile a jq program.

  2. Call an input method on the compiled program to supply the input.

  3. Call an output method on the result to retrieve the output.

For instance:

import jq

assert jq.compile(".+5").input_value(42).first() == 47

Input methods

Call .input_value() to supply a valid JSON value, such as the values returned from json.load:

import jq

assert jq.compile(".").input_value(None).first() == None
assert jq.compile(".").input_value(42).first() == 42
assert jq.compile(".").input_value(0.42).first() == 0.42
assert jq.compile(".").input_value(True).first() == True
assert jq.compile(".").input_value("hello").first() == "hello"

Call .input_values() to supply multiple valid JSON values, such as the values returned from json.load:

import jq

assert jq.compile(".+5").input_values([1, 2, 3]).all() == [6, 7, 8]

Call .input_text() to supply unparsed JSON text:

import jq

assert jq.compile(".").input_text("null").first() == None
assert jq.compile(".").input_text("42").first() == 42
assert jq.compile(".").input_text("0.42").first() == 0.42
assert jq.compile(".").input_text("true").first() == True
assert jq.compile(".").input_text('"hello"').first() == "hello"
assert jq.compile(".").input_text("1\n2\n3").all() == [1, 2, 3]

Pass slurp=True to .input_text() to read the entire input into an array:

import jq

assert jq.compile(".").input_text("1\n2\n3", slurp=True).first() == [1, 2, 3]

You can also call the older input() method by passing:

  • a valid JSON value, such as the values returned from json.load, as a positional argument

  • unparsed JSON text as the keyword argument text

For instance:

import jq

assert jq.compile(".").input("hello").first() == "hello"
assert jq.compile(".").input(text='"hello"').first() == "hello"

Output methods

Calling first() on the result will run the program with the given input, and return the first output element.

import jq

assert jq.compile(".").input_value("hello").first() == "hello"
assert jq.compile("[.[]+1]").input_value([1, 2, 3]).first() == [2, 3, 4]
assert jq.compile(".[]+1").input_value([1, 2, 3]).first() == 2

Call text() instead of first() to serialise the output into JSON text:

assert jq.compile(".").input_value("42").text() == '"42"'

When calling text(), if there are multiple output elements, each element is represented by a separate line:

assert jq.compile(".[]").input_value([1, 2, 3]).text() == "1\n2\n3"

Call all() to get all of the output elements in a list:

assert jq.compile(".[]+1").input_value([1, 2, 3]).all() == [2, 3, 4]

Call iter() to get all of the output elements as an iterator:

iterator = iter(jq.compile(".[]+1").input_value([1, 2, 3]))
assert next(iterator, None) == 2
assert next(iterator, None) == 3
assert next(iterator, None) == 4
assert next(iterator, None) == None

Arguments

Calling compile() with the args argument allows predefined variables to be used within the program:

program = jq.compile("$a + $b + .", args={"a": 100, "b": 20})
assert program.input_value(3).first() == 123

Convenience functions

Convenience functions are available to get the output for a program and input in one call:

assert jq.first(".[] + 1", [1, 2, 3]) == 2
assert jq.first(".[] + 1", text="[1, 2, 3]") == 2
assert jq.text(".[] + 1", [1, 2, 3]) == "2\n3\n4"
assert jq.all(".[] + 1", [1, 2, 3]) == [2, 3, 4]
assert list(jq.iter(".[] + 1", [1, 2, 3])) == [2, 3, 4]

Original program string

The original program string is available on a compiled program as the program_string attribute:

program = jq.compile(".")
assert program.program_string == "."

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

jq-1.6.0.tar.gz (2.9 MB view hashes)

Uploaded source

Built Distributions

jq-1.6.0-cp312-cp312-musllinux_1_1_x86_64.whl (661.9 kB view hashes)

Uploaded cp312

jq-1.6.0-cp312-cp312-musllinux_1_1_i686.whl (661.6 kB view hashes)

Uploaded cp312

jq-1.6.0-cp312-cp312-musllinux_1_1_aarch64.whl (638.8 kB view hashes)

Uploaded cp312

jq-1.6.0-cp312-cp312-macosx_11_0_arm64.whl (412.2 kB view hashes)

Uploaded cp312

jq-1.6.0-cp312-cp312-macosx_10_9_x86_64.whl (404.2 kB view hashes)

Uploaded cp312

jq-1.6.0-cp311-cp311-musllinux_1_1_x86_64.whl (666.3 kB view hashes)

Uploaded cp311

jq-1.6.0-cp311-cp311-musllinux_1_1_i686.whl (672.7 kB view hashes)

Uploaded cp311

jq-1.6.0-cp311-cp311-musllinux_1_1_aarch64.whl (648.6 kB view hashes)

Uploaded cp311

jq-1.6.0-cp311-cp311-macosx_11_0_arm64.whl (412.9 kB view hashes)

Uploaded cp311

jq-1.6.0-cp311-cp311-macosx_10_9_x86_64.whl (406.6 kB view hashes)

Uploaded cp311

jq-1.6.0-cp310-cp310-musllinux_1_1_x86_64.whl (662.0 kB view hashes)

Uploaded cp310

jq-1.6.0-cp310-cp310-musllinux_1_1_i686.whl (668.1 kB view hashes)

Uploaded cp310

jq-1.6.0-cp310-cp310-musllinux_1_1_aarch64.whl (642.4 kB view hashes)

Uploaded cp310

jq-1.6.0-cp310-cp310-macosx_11_0_arm64.whl (414.0 kB view hashes)

Uploaded cp310

jq-1.6.0-cp310-cp310-macosx_10_9_x86_64.whl (410.5 kB view hashes)

Uploaded cp310

jq-1.6.0-cp39-cp39-musllinux_1_1_x86_64.whl (665.9 kB view hashes)

Uploaded cp39

jq-1.6.0-cp39-cp39-musllinux_1_1_i686.whl (673.2 kB view hashes)

Uploaded cp39

jq-1.6.0-cp39-cp39-musllinux_1_1_aarch64.whl (648.3 kB view hashes)

Uploaded cp39

jq-1.6.0-cp39-cp39-macosx_11_0_arm64.whl (411.1 kB view hashes)

Uploaded cp39

jq-1.6.0-cp39-cp39-macosx_10_9_x86_64.whl (408.9 kB view hashes)

Uploaded cp39

jq-1.6.0-cp38-cp38-musllinux_1_1_x86_64.whl (681.7 kB view hashes)

Uploaded cp38

jq-1.6.0-cp38-cp38-musllinux_1_1_i686.whl (687.2 kB view hashes)

Uploaded cp38

jq-1.6.0-cp38-cp38-musllinux_1_1_aarch64.whl (664.3 kB view hashes)

Uploaded cp38

jq-1.6.0-cp38-cp38-macosx_10_9_x86_64.whl (405.3 kB view hashes)

Uploaded cp38

jq-1.6.0-cp37-cp37m-musllinux_1_1_x86_64.whl (636.6 kB view hashes)

Uploaded cp37

jq-1.6.0-cp37-cp37m-musllinux_1_1_i686.whl (645.8 kB view hashes)

Uploaded cp37

jq-1.6.0-cp37-cp37m-musllinux_1_1_aarch64.whl (618.5 kB view hashes)

Uploaded cp37

jq-1.6.0-cp37-cp37m-macosx_10_9_x86_64.whl (403.5 kB view hashes)

Uploaded cp37

jq-1.6.0-cp36-cp36m-musllinux_1_1_x86_64.whl (636.9 kB view hashes)

Uploaded cp36

jq-1.6.0-cp36-cp36m-musllinux_1_1_i686.whl (645.2 kB view hashes)

Uploaded cp36

jq-1.6.0-cp36-cp36m-musllinux_1_1_aarch64.whl (618.3 kB view hashes)

Uploaded cp36

jq-1.6.0-cp36-cp36m-macosx_10_9_x86_64.whl (403.1 kB view hashes)

Uploaded cp36

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page