jq (Python bindings)
jq is a lightweight and flexible JSON processor. This Python library provides robust bindings to the native `jq` C library (version 1.8.1), allowing Python applications to compile and execute `jq` programs for efficient JSON manipulation, filtering, and transformation. Currently at version 1.11.0, the project maintains an active release cadence with regular updates.
Warnings
- deprecated The `input()` method on compiled `jq` programs is older and deprecated. It can accept a valid JSON value as a positional argument or unparsed JSON text via the `text` keyword argument.
- gotcha When `pip install jq` is performed without pre-built wheels available for your specific Python version and architecture, the package attempts to build from source. This requires native development tools (e.g., `autoconf`, `automake`, `libtool`, `build-essential` on Linux, Xcode command line tools on macOS) and Python development headers to be installed on your system.
- gotcha There are multiple Python bindings for `jq`, notably `jq` (mwilliamson/jq.py) and `pyjq` (doloopwhile/pyjq). These libraries are incompatible and have different APIs. Accidentally installing or importing the wrong one can lead to unexpected errors.
- gotcha Certain `jq` language constructs, such as `first`, `last`, `nth(n)`, and excessive `slurping` without proper optimization, can lead to inefficient or unexpected behavior in complex `jq` programs, especially with large datasets.
Install
-
pip install jq -
sudo apt-get install autoconf automake build-essential libtool python3-dev && pip install jq
Imports
- jq
import jq
Quickstart
import jq
json_data = {"name": "Alice", "age": 30, "city": "New York"}
# Compile a jq program to extract the name and add a greeting
compiled_program = jq.compile(".name | ""Hello, \(. )!""")
# Process a single JSON value
result = compiled_program.input_value(json_data).first()
print(result)
# Example with a list of values
list_data = [{"value": 1}, {"value": 2}]
compiled_list_program = jq.compile(".value + 10")
results_list = compiled_list_program.input_values(list_data).all()
print(results_list)