JSONiq

XQuery for JSON, JSON for XQuery

JSON is a lot simpler than XML in many data-oriented applications, and it is widely used in Web Service APIs and data feeds. But JSON has no full-featured query language, which means it is no longer simple for the most demanding applications.

JSONiq is a query language for JSON, based on XQuery. One profile of JSONiq removes everything directly related to XML, adding JSON constructors and navigation. Another profile includes the full XQuery language, with added JSON support, allowing queries to consume or produce JSON, XML, or HTML.

Who is JSONiq?

The JSONiq web site and the JSONiq language belong to the authors of the specification: Jonathan Robie, Matthias Brantner, Daniela Florescu, Ghislain Fourny, and Till Westmann. This work is independent of the companies we work for. JSONiq is licensed under a Creative Commons Attribution-NoDerivs 3.0 Unported License.

To communicate with us, please use our Google Group:

Subscribe to JSONiq
Email:
Visit this group

JSONiq Specifications

These documents describe JSONiq:

JSONiq Use Cases PDF, HTML, HTML (single page)
JSONiq Language Specification PDF, HTML, HTML (single page)
JSONiq XQ-- Profile (JSON-only) XQ-- Grammar (Railroad Diagrams in HTML)
JSONiq XQ++ Profile (XQuery + JSON) XQ++ Grammar (Railroad Diagrams in HTML)

Also see the presentation at http://jsoniq.org/presentations/overview-2011Oct/JSONiq.xhtml.



JSONiq for JSON-only Environments

Suppose a social media site models friends using JSON objects like this:

{
  "name" : "Sarah",
  "age" : 13,
  "gender" : "female",
  "friends" : [ "Jim", "Mary", "Jennifer"]
}

JSONiq lets you find JSON objects, extract the data you need, create new JSON objects using that data, and create completely new JSON structures in many different ways, using the full power of XQuery.

The following JSONiq query creates a new user named "Jennifer", one year older than Sarah, with a friend list based on Sarah's. Jennifer does not appear on her own friends list, but Sarah does:

let $sarah := collection("users")[.("name") = "Sarah"]
return {
    "name" : "Jennifer",
    "age"  : $sarah("age") + 1,
    "friends" : [ values($sarah("friends"))[. != "Jennifer"], "Sarah" ]
}

The result of the above query is:

{
    "name" : "Jennifer",
    "age"  : 14,
    "friends" : [ "Jim", "Mary", "Sarah"]
}


JSONiq for JSON + XML + HTML

XQuery can be used to query or produce XML or HTML, and can also be used to query most relational databases. When JSONiq extensions are used together with the full XQuery language, a query can use or produce data in many different formats.

For instance, suppose the following JSON data needs to be converted to HTML:

{
  "col labels" : ["singular", "plural"],
  "row labels" : ["1p", "2p", "3p"],
  "data" :
     [
        ["spinne", "spinnen"],
        ["spinnst", "spinnt"],
        ["spinnt", "spinnen"]
     ]
}

The following query creates an HTML table from this JSON Object, using the column headings and row labels specified:

<table>
  <tr> (: Column headings :)
     {
        <th> </th>,
        for $th in values(json("table.json")("col labels"))
        return <th>{ $th }</th>
     }
  </tr>
  {  (: Data for each row :)
     for $r at $i in values(json("table.json")("data"))
     return
        <tr>
         {
           <th>{ values(json("table.json")("row labels")[$i]) }</th>,
           for $c in $r
           return <td>{ $c }</td>
         }
        </tr>
  }
</table>

The result of the query is the following HTML table:

<table>
   <tr>
     <th> </th>
     <th>singular</th>
     <th>plural</th>
   </tr>
   <tr>
     <th>1p</th>
     <td>spinne</td>
     <td>spinnen</td>
   </tr>
   <tr>
     <th>2p</th>
     <td>spinnst</td>
     <td>spinnt</td>
   </tr>
   <tr>
     <th>3p</th>
     <td>spinnt</td>
     <td>spinnen</td>
   </tr>
</table>
    

In a web browser, the table looks like this:

singular plural
1p spinne spinnen
2p spinnst spinnt
3p spinnt spinnen