jq (programming language)
![]() | |
Paradigm | Purely functional programming, JSON-oriented processing, tacit programming |
---|---|
Designed by | Stephen Dolan |
First appeared | 2012 |
Stable release | C version: 1.6; Go version: 0.12.10
/ C version: November 18, 2018 ; Go version = December 1, 2022 |
Typing discipline | dynamic |
Website | stedolan |
Major implementations | |
jq, gojq | |
Influenced by | |
Icon, Haskell |
jq is a very high-level functional programming language with support for backtracking and managing 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, zero-arity functions can be used as parameters of other functions.
Although there is no official standard for jq, a de facto standard is defined by the substantial agreement between the C and Go-based versions.
Two Parsing Modes
In addition to a regular JSON parser that supports the sequential parsing of potentially very long streams of JSON documents, jq also has an incremental parser -- a so-called "streaming parser" -- which amongst other things minimizes the memory requirements for processing very large large JSON entities.
Syntax
The jq language is based on the same concepts of filters, streams, and pipes that are familiar from the Unix shell.
Every valid JSON expression can be used as a filter, and a JSON-like syntax is also used for constructing jq objects and arrays.
More complex expressions can be built by connecting filters together with the pipe character "|
".
For example, the identity filter is ".
", so that the expression 1 | {"a": .}
would produce the JSON value: {"a": 1}
.
There are special syntactic forms for function creation, conditionals, stream reduction, and the module system.
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:
def tobase($b):
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1];
def div: (. / $b) | floor;
def mod: . % $b;
def r: if . < $b then digit else (div | r) + (mod | digit) end;
#
select(2 <= $b and $b <= 36)
| r ;
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): ([range(m;n+1)] - 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;[$s]) as $e
| choose(2;9;[$s,$e]) as $n
| choose(2;9;[$s,$e,$n]) as $d
| choose(2;9;[$s,$e,$n,$d]) as $r
| choose(2;9;[$s,$e,$n,$d,$r]) as $y
| select(num($s;$e;$n;$d) + num($m;$o;$r;$e) == num($m;$o;$n;$e;$y))
| [$s,$e,$n,$d,$m,$o,$r,$e,$m,$o,$n,$e,$y] );
See also
References
The jq manual and the jq wiki are the main jq references.
External links
- jq homepage
- jq source code
- gojq - the Pure Go implementation
- Awesome jq - curated listing of jq-related resources
- The jq Programming Language page on the Rosetta Code comparative programming tasks project site