Jq (programming Language)
   HOME

TheInfoList



OR:

jq is a very high-level
lexically scoped In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...
functional
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
in which every
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
Object Notation (
JSON JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
) value is a constant. jq supports
backtracking Backtracking is a class of algorithms for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it de ...
and managing indefinitely long
streams A stream is a continuous body of surface water flowing within the bed and banks of a channel. Depending on its location or certain characteristics, a stream may be referred to by a variety of local or regional names. Long, large stream ...
of JSON data. It is related to two programming languages—
Icon An icon () is a religious work of art, most commonly a painting, in the cultures of the Eastern Orthodox, Oriental Orthodox, Catholic Church, Catholic, and Lutheranism, Lutheran churches. The most common subjects include Jesus, Mary, mother of ...
and
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
. The language supports a
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
-based module system and has some support for closures. functions and functional expressions can be used as parameters of other functions. The original implementation of jq was in
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
before being quickly 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 A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of character (computing), characters that specifies a pattern matching, match pattern in string (computer science), text. Usually ...
was added in jq version 1.5. A "wrapper" program for jq name
yq
adds support for
YAML YAML ( ) is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Marku ...
,
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
and
TOML Tom's Obvious, Minimal Language (TOML, originally ''Tom's Own Markup Language'') is a file format for configuration files. It is intended to be easy to read and write due to obvious semantics which aim to be "minimal", and it is designed to map u ...
. It was first released in 2017. The Go implementation, gojq, was initially released in 2019. gojq notably extends jq to include support for
YAML YAML ( ) is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Marku ...
. The
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
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 cURL (pronounced like "curl", ) is a free and open source computer program for transferring data to and from Internet servers. It can download a URL from a web server over HTTP, and supports a variety of other network protocols, URI scheme ...
. Here is an example showing how the output of a command can be piped to a jq filter to determine the category names associated with this
Wikipedia Wikipedia is a free content, free Online content, online encyclopedia that is written and maintained by a community of volunteers, known as Wikipedians, through open collaboration and the wiki software MediaWiki. Founded by Jimmy Wales and La ...
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 command above uses the
MediaWiki MediaWiki is free and open-source wiki software originally developed by Magnus Manske for use on Wikipedia on January 25, 2002, and further improved by Lee Daniel Crocker,mailarchive:wikipedia-l/2001-August/000382.html, Magnus Manske's announc ...
API for this page to produce a JSON response. The pipe allows the output of to be accessed by jq, a standard
Unix shell A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
mechanism. The jq filter shown is an abbreviation for the jq pipeline: . parse", . categories", .[] , .["*"] This corresponds to the nested JSON structure produced by the call to . 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 functions can be embedded in other applications and programming environments. For example, gojq has been integrated with
SQLite SQLite ( "S-Q-L-ite", "sequel-ite") is a free and open-source relational database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it ...
so that a function is available in
SQL Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel") is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
statements. This function is marked a
"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 ath, valuearrays for all "leaf" paths. The ''streaming parser'' is very useful when one of more of the JSON inputs is too large to fit in memory, since its memory needs are usually quite small. For example, for an arbitrarily large array of JSON objects, the peak memory need is little more than needed 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 also a value in jq, which accordingly has the
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s 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.

is a value, just like any other JSON scalar; it is not a pointer or a "null-pointer". (corresponding to NaN) and (see
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard #Design rationale, add ...
) are the only two jq scalars that are not also JSON values.


Forms

Special
syntactic In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituency ...
forms exist for function creation, conditionals, stream reduction, and the module system.


Filters

Here is an example of defining 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" :.+1 def mod: . % $b; def div: ((. - mod) / $b); def digits: recurse( select(. >= $b) , div) , mod ; select(2 <= $b and $b <= 36) , digit, reverse , add; The next example demonstrates the use of generators in the classic "SEND MORE MONEY"
verbal arithmetic Verbal arithmetic, also known as alphametics, cryptarithmetic, cryptarithm or word addition, is a type of mathematical game consisting of a mathematical equation among unknown numbers, whose numerical digit, digits are represented by Letter (alpha ...
game: def send_more_money: def choose(m;n;used): ( ange(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);


Parsing expression grammars

There is a very close relationship between jq and the
parsing expression grammar In computer science, a parsing expression grammar (PEG) is a type of analytic formal grammar, i.e. it describes a formal language in terms of a set of rules for recognizing strings in the language. The formalism was introduced by Bryan Ford in 20 ...
(PEG) formalism. The relationship stems from the equivalence of the seven basic PEG operations and the jq constructs shown in the following table.


Ports and variants

gojq is a '' pure Go'' implementation. There is also a
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
implementation of a dialect of jq named jaq for which a
denotational semantics In computer science, denotational semantics (initially known as mathematical semantics or Scott–Strachey semantics) is an approach of formalizing the meanings of programming languages by constructing mathematical objects (called ''denotations'' ...
has been specified.


Notes


References


Bibliography

* * *


Others


External links

* * – Pure Go implementation * – Rust implementation * – jq implementation * – jq FAQ * – Awesome jq - curated list of jq-related resources
The jq Programming Language page
on the
Rosetta Code Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
comparative programming tasks project site {{DEFAULTSORT:jq (Programming Language) Dynamically typed programming languages Functional languages Programming languages Programming languages created in 2012 Query languages 2012 software Articles with example code