Jump to content

jq (programming language)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peak (talk | contribs) at 04:51, 19 December 2022 (jq and gojq). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
jq
ParadigmPurely functional programming, JSON-oriented processing, tacit programming
Designed byStephen Dolan
First appeared2012; 13 years ago (2012)
Stable release
C version: 1.6; Go version: 0.12.10 / C version: November 18, 2018; 6 years ago (2018-11-18); Go version = December 1, 2022; 2 years ago (2022-12-01)
Typing disciplinedynamic
Websitestedolan.github.io/jq/ github.com/itchyny/gojq
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.