The official jq logo | |
Paradigms | Purely functional programming, JSON-oriented processing, tacit programming |
---|---|
Designed by | Stephen Dolan |
First appeared | August 21, 2012; 12 years ago (2012-08-21) |
Stable release | 1.7.1 / December 13, 2023; 12 months ago (December 13, 2023) |
Implementation language | jq: C gojq: Go jaq: Rust jqjq: jq |
Platform | Cross-platform |
OS | Cross-platform |
License | MIT |
Website | jqlang |
jq is a very high-level lexically scoped functional programming language in which every JSON value is a constant. jq supports backtracking and managing indefinitely long streams of JSON data. It is related to the Icon and Haskell programming languages. The language supports a namespace-based module system and has some support for closures. In particular, functions and functional expressions can be used as parameters of other functions.
The original implementation of jq was in Haskell before being immediately ported to C.
History
jq was created by Stephen Dolan, and released in October 2012. It was described as being "like sed for JSON data". Support for regular expressions was added in jq version 1.5.
A "wrapper" program for jq named yq adds support for YAML, XML and TOML. It was first released in 2017.
The Go implementation, gojq, was initially released in 2019. gojq notably extends jq to include support for YAML.
The Rust implementation, jaq, has as its project goals a faster and more correct implementation of jq, while preserving compatibility with jq in most cases. Explicitly excluded from the project goals as of March 2024 are certain advanced features of jq such as modules, SQL-style operators, and a streaming parser for very large JSON documents.
The jq implementation, jqjq, was initially released in 2022. jqjq notably can run itself, has a REPL and supports eval.
Usage
Command-line usage
jq is typically used at the command line and can be used with other command-line utilities, such as curl. Here is an example showing how the output of a curl
command can be piped to a jq filter to determine the category names associated with this Misplaced Pages page:
$ curl -s 'https://en.wikipedia.org/w/api.php?action=parse&page=jq_(programming_language)&format=json' | jq '.parse.categories."*"'
The output produced by this pipeline consists of a stream of JSON strings, the first few of which are:
"Articles_with_short_description" "Short_description_matches_Wikidata" "Dynamically_typed_programming_languages" "Functional_languages" "Programming_languages" "Programming_languages_created_in_2012" "Query_languages" "2012_software"
The curl
command above uses the MediaWiki API for this page to produce a JSON response.
The pipe |
allows the output of curl
to be accessed by jq, a standard Unix shell mechanism.
The jq filter shown is an abbreviation for the jq pipeline:
. | . | . | .
This corresponds to the nested JSON structure produced by the call to curl
. Notice that the jq pipeline is constructed in the same manner using the |
character as the Unix-style pipeline.
Embedded usage
Both the C and the Go implementations provide libraries so that jq functionality can be embedded in other applications and programming environments.
For example, gojq has been integrated with SQLite so that a jq
function is available in SQL statements. This function is marked as
"deterministic" and
can therefore be used in "CREATE INDEX" commands.
Modes of operation
jq by default acts as a "stream editor" for JSON inputs, much like the sed utility can be thought of as a "stream editor" for lines of text. However jq has several other modes of operation:
- it can treat its input from one or more sources as lines of text;
- it can gather a stream of inputs from a specified source into a JSON array;
- it can parse its JSON inputs using a so-called "streaming parser" that produces a stream of arrays for all "leaf" paths.
The "streaming parser" is particularly useful when one of more of the JSON inputs is too large to fit into memory, since its memory requirements are typically quite small. For example, for an arbitrarily large array of JSON objects, the peak memory requirement is not much more than required to handle the largest top-level object.
These modes of operation can, within certain limitations, be combined.
Syntax and semantics
Types
Every JSON value is itself a value in jq, which accordingly has the types shown in the table below. The gojq and jaq implementations distinguish between integers and non-integer numbers. The gojq implementation supports unbounded-precision integer arithmetic, as did the original implementation of jq in Haskell.
Type | Examples |
---|---|
"number" |
|
"string" |
|
"boolean" |
|
"array" |
|
"object" |
|
"null" |
|
null
is a value, just like any other JSON scalar; it is not a pointer or a "null-pointer".
nan
(corresponding to NaN) and infinite
(see IEEE 754) are the only two jq scalars that are not also JSON values.
Forms
There are special syntactic forms for function creation, conditionals, stream reduction, and the module system.
Filters
Here is an example which shows how to define a named, parameterized filter for formatting an integer in any base from 2 to 36 inclusive. The implementation illustrates tacit (or point-free) programming:
# Use gojq for infinite precision integer arithmetic def tobase($b): def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; def mod: . % $b; def div: ((. - mod) / $b); def digits: recurse( select(. >= $b) | div) | mod ; select(2 <= $b and $b <= 36) | | reverse | add;
The next example demonstrates the use of generators in the classic "SEND MORE MONEY" verbal arithmetic game:
def send_more_money: def choose(m;n;used): ( - used); def num(a;b;c;d): 1000*a + 100*b + 10*c + d; def num(a;b;c;d;e): 10*num(a;b;c;d) + e; first( 1 as $m | 0 as $o | choose(8;9;) as $s | choose(2;9;) as $e | choose(2;9;) as $n | choose(2;9;) as $d | choose(2;9;) as $r | choose(2;9;) as $y | select(num($s;$e;$n;$d) + num($m;$o;$r;$e) == num($m;$o;$n;$e;$y)) | );
Parsing expression grammars
There is a very close relationship between jq and the parsing expression grammar (PEG) formalism. The relationship stems from the equivalence of the seven basic PEG operations and the jq constructs shown in the following table.
PEG operation name | PEG notation | jq operation or def |
---|---|---|
Sequence | e1 e2
|
e1 | e2
|
Ordered choice | e1 / e2
|
e1 // e2
|
Zero-or-more | e*
|
def star(E): (E | star(E)) // . ;
|
One-or-more | e+
|
def plus(E): E | (plus(E) // . );
|
Optional | e?
|
def optional(E): E // .;
|
And-predicate | &e
|
def amp(E): . as $in | E | $in;
|
Not-predicate | !e
|
def neg(E): select( == );
|
Ports and variants
gojq is a "pure Go" implementation. There is also a Rust implementation of a dialect of jq named jaq for which a denotational semantics has been specified.
Notes
- Neither the C nor the Go implementations of jq has any runtime dependencies.
- Including Windows, Linux, and macOS. The Go implementation can be compiled on any platform on which Go is supported.
- The C implementation of jq uses a decimal floating-point library known as decNumber, which is licensed under the ICU license; and the Oniguruma regex library, which has a BSD license.
References
Bibliography
- Janssens, Jeroen (2021). Data Science at the Command Line. O'Reilly Media. ISBN 9781492087885.
- Janssens, Jeroen (2014). Data Science at the Command Line: Facing the Future with Time-Tested Tools. O'Reilly Media. ISBN 9781491947807.
- Marrs, Tom (2017). JSON at Work: Practical Data Integration for the Web. O'Reilly Media. ISBN 9781491982419.
Others
- "Release jq 1.7.1".
- ^ "Download jq". jq. Retrieved January 6, 2023.
- "Initial · jqlang/Jq@eca89ac". GitHub.
- Janssens 2014.
- "jq". jq. Retrieved January 6, 2023.
- "like sed". Archived from the original on 2013-04-14.
- "Release v2.0.0 · kislyuk/yq". GitHub.
- "Release v0.0.1 · itchyny/gojq". GitHub.
- ^ "01mf02/jaq: A jq clone focussed on correctness, speed, and simplicity". GitHub. Retrieved March 6, 2024.
- "Tutorial". jq. Retrieved January 6, 2023.
- "sqlite_jq". GitHub.
- "FAQ". GitHub.
- "Manual". jq. Retrieved January 6, 2023.
- "PEG". PEG.
- Färber, Michael (2023). "Denotational Semantics and a fast interpreter for jq". arXiv:2302.10576 .
External links
- jq homepage
- gojq - the Pure Go implementation
- jaq - the Rust implementation
- jqjq - the jq implementation
- jq FAQ
- Awesome jq - curated listing of jq-related resources
- The jq Programming Language page on the Rosetta Code comparative programming tasks project site