JSONiq
   HOME

TheInfoList



OR:

JSONiq is a query and
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions tha ...
language that is designed to declaratively query and transform collections of hierarchical and heterogeneous data in format of
JSON JSON (JavaScript Object Notation, pronounced ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other se ...
,
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. T ...
, as well as unstructured, textual data. JSONiq is an open specification published under the Creative Commons Attribution-ShareAlike 3.0 license. It is based on the
XQuery XQuery (XML Query) is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, b ...
language, with which it shares the same core expressions and operations on atomic types. JSONiq comes in two syntactical flavors, which both support JSON and XML natively. # The JSONiq syntax (a superset of JSON) extended with XML support through a compatible subset of XQuery. # The XQuery syntax (native XML support) extended with JSON support through a compatible subset (the JSONiq extension to XQuery) of the above JSONiq syntax.


Features

JSONiq primarily provides means to extract and transform data from JSON documents or any data source that can be viewed as JSON (e.g.
relational database A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relati ...
s or web services). The major expression for performing such operations is the SQL-like “
FLWOR The programming language XQuery defines FLWOR (pronounced 'flower') as an expression that supports iteration and binding of variables to intermediate results. FLWOR is an acronym: FOR, LET, WHERE, ORDER BY, RETURN. FLWOR is loosely analogous to ...
expression” that comes from XQuery. A FLWOR expression is constructed from the five clauses after which it is named: FOR, LET, WHERE, ORDER BY, RETURN. However, it also supports clauses for doing grouping and windowing. The language also provides syntax for constructing new JSON documents where either the field names and values are known in advance or can be computed dynamically. The JSONiq language (not the extension to XQuery) is a superset of JSON. That is, each JSON document is a valid JSONiq program. Additionally, the language also supports a navigational syntax for extracting field names and values out of JSON objects as well as values out of JSON arrays. Navigation is resilient in the absence of values, or if values are heterogeneous, in that it silently ignores unforeseen values without raising errors. All constructs are defined as expressions within the language and can be arbitrarily nested. JSONiq does not include features for updating JSON or XML documents, it does not have full text search capabilities, and has no statements. All of these features are under active development for a subsequent version of the language. JSONiq is a programming language that can express arbitrary JSON to JSON or XML to XML transformations. It also allows for transformations between JSON and XML. All such transformations have the following features: #Logical/physical data independence #Declarative #High level #Side-effect free #Strongly typed


Data model

The language is based on the JSONiq Data Model (JDM) which is an extension of the
XQuery and XPath Data Model The XQuery and XPath Data Model (XDM) is the data model shared by the XPath 2.0, XSLT 2.0, XQuery, and XForms programming languages. It is defined in a W3C recommendation.Anders Berglund, ''et al.''XQuery 1.0 and XPath 2.0 Data Model W3C, 2010, re ...
(XDM). The JDM uses a tree-structured model of the information content of a JSON or XML document. It contains JSON objects, JSON arrays, all kinds of XML nodes, as well as atomic values such as integers, strings, or boolean all being defined in
XML Schema An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntactical constraints imposed by XML itself. These constra ...
. JDM forms the basis for a set-oriented language, in that instances of the data model are sequences (a singleton value is considered to be a sequence of length one). The items in a sequence can be JSON objects, JSON arrays, XML nodes, or atomic values.


Examples

The sample JSONiq code below computes the area code and the number of all people older than 20 from a collection of JSON person objects (see the
JSON JSON (JavaScript Object Notation, pronounced ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other se ...
article for an example object). for $p in collection("persons") where $p.age gt 20 let $home := $p.phoneNumber[][$$.type eq "home"].number group by $area := substring-before($home, " ") return All JSONiq constructs are expressions and can also be contained in the body of a function. declare function local:adults() ; The next query transforms parts of each person object into an XML element using the XQuery syntax (JSONiq extension to XQuery). for $p in collection("persons") return


Applications

Below are a few examples of how and where JSONiq can be used: #Extracting information out of a database to use in a web service. #Generating summary reports on data stored in a JSON document store. #Selecting and transforming JSON data to XHTML to be published on the Web. #Correlating data from various sources and formats (e.g. JSON document store, XML database, relational database, and web service) and offer it in a web service. #Transforming collections of JSON objects into a different schema.


Comparison of the two syntactic flavors

There are two syntaxes of JSONiq, which users can use on whether they are focusing on JSON or XML. Both syntaxes use the same data model and are very similar up to a few exceptions.


JSONiq syntax

The pure JSONiq syntax is a superset of JSON. It is not strictly speaking a superset of XQuery even though all its expressions and semantics are available. The following aspects of the JSONiq syntax are not XQuery conformant: #No names containing dots. #No . for the context item ($$ has to be used instead). #No single-quoted literals. #JSON, backslash-based escaping in
string literal A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...
s. #No axis step allowed at the beginning of a relative path expression.


XQuery syntax with JSONiq extension

The JSONiq extension to XQuery is a superset of XQuery but not a superset of JSON. It is fully conformant and backwards compatible with XQuery 3.0 candidate recommendation. The following aspects of JSONiq are not supported in the XQuery syntax. #No dot-based object lookup ($object(“key”) instead). #No $$ for the context item. #XML, ampersand-based escaping of string literals. #Object keys must be quoted #No true/false/null literals #Built-in atomic types must be prefixed with xs:. #Non-atomic types must be followed by parentheses. #The empty-sequence() must be written as such. #No array lookup and no [] array unboxing.


Further reading

* JSONiq - The SQL of NoSQL. Ghislain Fourny. CreateSpace Independent Publishing Platform. .


Implementations

* Zorba (XQuery processor), Zorba: JSONiq and JSONiq Extension to XQuery
IBM WebSphere
JSONiq extension to XQuery

JSONiq extension to XQuery


References

{{Reflist JSON Functional languages