jq (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Jq logo.png
ParadigmPurely functional programming, JSON processing oriented
Designed byStephen Dolan
First appeared2012; 7 years ago (2012)
Stable release
1.6 / November 2, 2018; 2 months ago (2018-11-02)[1]
Typing disciplinedynamic
Major implementations
Influenced by
Icon, Haskell, Unix shell

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.

Basic syntax[edit]

The jq language is based on the same concepts of streams, pipes, and filters that are familiar from the Unix shell. Filters can be constructed from valid JSON expressions using a JSON-oriented syntax and are connected using the pipe character "|". The identity filter is ".", so that for example the expression 1 | {"a": .} would produce the JSON value: {"a": 1}.

Here is an example showing how to define a named, parameterized filter for formatting an integer in any base. The implementation illustrates tacit 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 a classical 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;
    label $exit
    | 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],
      break $exit

See also[edit]


  1. ^ "Releases - stedolan/jq". Retrieved 4 November 2018 – via GitHub.


The jq manual and the jq wiki are the main jq references.

External links[edit]