• Stars
    star
    360
  • Rank 118,230 (Top 3 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python bindings for jq

jq.py: a lightweight and flexible JSON processor

This project contains Python bindings for jq.

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.6 is downloaded over HTTPS and built. This requires:

  • Autoreconf
  • The normal C compiler toolchain, such as gcc and make.
  • libtool
  • Python headers.

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 == "."

More Repositories

1

mammoth.js

Convert Word documents (.docx files) to HTML
JavaScript
4,903
star
2

python-mammoth

Convert Word documents (.docx files) to HTML
Python
785
star
3

spur.py

Run commands and manipulate files locally or over SSH using the same interface
Python
266
star
4

java-mammoth

Convert Word documents to simple and clean HTML
Java
248
star
5

python-precisely

Matcher library for Python
Python
237
star
6

stickytape

Convert Python packages into a single script
Python
224
star
7

dotnet-mammoth

Convert Word documents to simple and clean HTML (C#/.NET)
C#
145
star
8

python-vendorize

Vendorize packages from PyPI
Python
95
star
9

whack

Compile and run relocatable Linux programs
Python
86
star
10

node-graphjoiner

Implementing GraphQL with joins to avoid the N+1 problem
JavaScript
47
star
11

python-graphjoiner

Implementing GraphQL with joins to avoid the N+1 problem
Python
45
star
12

python-makefile

Standard makefile for my Python projects
Makefile
36
star
13

python-graphlayer

Python
26
star
14

node-options

The option type, also known as the maybe type, for JavaScript
JavaScript
23
star
15

locket.py

File-based locks for Python on Linux and Windows
Python
23
star
16

mammoth-wordpress-plugin

WordPress plugin to convert docx files into posts
JavaScript
21
star
17

node-license-sniffer

Detect the license of node.js projects and their dependencies
JavaScript
18
star
18

mintaka

Mintaka: Run long-running processes in parallel, automatically focus on problems
Rust
10
star
19

http-api-proxy

A proxy for APIs to allow rate-limiting and caching
JavaScript
8
star
20

sdmx.py

Read SDMX XML files using Python
Python
7
star
21

lop

Parsing for JavaScript
JavaScript
7
star
22

setup-wabt-action

JavaScript
6
star
23

python-cobble

Create Python data objects
Python
5
star
24

farthing

Python
5
star
25

node-sql-gen

A SQL query builder for node.js, inspired by SQLAlchemy
TypeScript
5
star
26

ttrpg-map-sketcher

TypeScript
4
star
27

java-couscous

Convert Java source code to other languages. C# and Python currently supported.
Java
4
star
28

node-rate-limit

Simple rate limited queues for node.js
JavaScript
4
star
29

peachtree

Python library to interact with qemu/kvm
Python
4
star
30

setup-wasmtime-action

GitHub action for wasmtime
JavaScript
4
star
31

node-dough

Simple dependency injection for node.js
JavaScript
4
star
32

mayo

Download source control URIs in Python
Python
3
star
33

dingbat-to-unicode

Mapping from Dingbat fonts, such as Symbol, Webdings and Wingdings, to Unicode code points
TypeScript
3
star
34

clunk

Java
3
star
35

whack-package-python-virtualenv-env

Create relocatable (aka path-independent) Python virtualenvs using Whack
Shell
2
star
36

chembl-graphql

Python
2
star
37

node-glimpse-connect

Glimpse for Connect on node.js
JavaScript
2
star
38

python-tempman

Create and clean up temporary directories
Python
2
star
39

duck.js

Rich matchers for JavaScript, inspired by Hamcrest
JavaScript
2
star
40

dodge.py

Python
1
star
41

vde

C
1
star
42

web-widgets-knockout

Use generic JavaScript widgets in Knockout, and create generic widgets using Knockout
JavaScript
1
star
43

beach

Python
1
star
44

conan-bdwgc

Conan recipe for Boehm-Demers-Weiser garbage collector
Python
1
star
45

node-fs-in-memory

An in-memory implementation of the fs module in node.js, intended for testing purposes
JavaScript
1
star
46

starboard.py

Utilities to help with networking
Python
1
star
47

patter

Utilities for iterating through arrays using promises
JavaScript
1
star
48

whack-run

C
1
star
49

funk

A mocking framework for Python, influenced by JMock
Python
1
star
50

nope

Python
1
star
51

windows-make-demo

Makefile
1
star