Please refer to the errata for this document, which may include some normative corrections.
See also translations.
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
SPARQL is a set of standards for the query and update of RDF data, along with ways to access such data over the web. This document describes the representation of SELECT and ASK query results using JSON.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is one of eleven SPARQL 1.1 Recommendations produced by the SPARQL Working Group:
There have been no substantive changes to this document since the previous version. Minor editorial changes, if any, are detailed in the change log and visible in the color-coded diff.
Please send any comments to [email protected] (public archive). Although work on this document by the SPARQL Working Group is complete, comments may be addressed in the errata or in future revisions. Open discussion is welcome at [email protected] (public archive).
This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
1 Introduction
2 JSON Results Object
3 Variable Binding Results
3.1 "head"
3.1.1 "vars"
3.1.2 "link"
3.2 "results"
3.2.1 "bindings"
3.2.2 Encoding RDF terms
4 Boolean Results
4.1 "head"
4.1.1 "link"
4.2 "boolean"
5 Example
6 Internet Media Type, File Extension and Macintosh File Type
A References
A.1 Normative References
A.2 Other References
This document describes how to serialize SPARQL results (SELECT and ASK query forms) in a JSON format. The format is designed to be a complete representation of the information in the query results. The results of a SELECT query are serilialized as an array, where each array element is one "row" of the query results; the results of an ASK query give the boolean value of the query result.
An Internet Media Type is provied for application/sparql-results+json
.
There is also a SPARQL Query Results XML Format [SRX] which follows a similar design pattern but uses XML as the serialization.
Unless otherwise noted in the section heading, all sections and appendices in this document are normative.
The results of a SPARQL Query are serialized in JSON as a single top-level JSON object. This object has a "head"
member and either a "results"
member or a "boolean"
member, depending on the query form.
This example shows the results of a SELECT
query. The query solutions are represented in an array which is the value of the "bindings"
key, in turn part of an object that is the value of the "results"
key:
{ "head": { "vars": [ "book" , "title" ] } , "results": { "bindings": [ { "book": { "type": "uri" , "value": "http://example.org/book/book6" } , "title": { "type": "literal" , "value": "Harry Potter and the Half-Blood Prince" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book7" } , "title": { "type": "literal" , "value": "Harry Potter and the Deathly Hallows" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book5" } , "title": { "type": "literal" , "value": "Harry Potter and the Order of the Phoenix" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book4" } , "title": { "type": "literal" , "value": "Harry Potter and the Goblet of Fire" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book2" } , "title": { "type": "literal" , "value": "Harry Potter and the Chamber of Secrets" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book3" } , "title": { "type": "literal" , "value": "Harry Potter and the Prisoner Of Azkaban" } } , { "book": { "type": "uri" , "value": "http://example.org/book/book1" } , "title": { "type": "literal" , "value": "Harry Potter and the Philosopher's Stone" } } ] } }
This example shows the result from an ASK
query:
{ "head" : { } , "boolean" : true }
Other keys, with different names, may be present in the JSON Results Object but are not defined by this specification.
The results of a SPARQL SELECT
query are serialized as
an array of bindings of variables. The value of the "head"
key is an array of all variables projected in the query's SELECT
clause.
The "head"
member gives the variables mentioned in the results and
may contain a "link"
member.
{ "head" { "vars" : [ ... ] , "link" : [ ... ] }
The "vars"
member is an array giving the names of the variables used in the results.
These are the projected variables from the query.
A variable is not necessarily given a value in every query solution of the results.
"vars" : [ "book" , "title" ]
The order of variable names should correspond to the variables in the SELECT clause of the query,
unless the query is of the form SELECT *
in which case order is not significant.
The value of the "results"
member is an object with a single key, "bindings"
.
The value of the "bindings"
member is an array with zero
or more elements, one element per query solution.
Each query solution is a JSON object. Each key
of this object is a variable name from the query solution. The value for
a given variable name is a JSON object that
encodes the variable's bound value, an RDF term.
There are zero elements in the array if the query returned an
empty solution sequence.
Variables names do not include the initial "?" or "$" character.
Each variable name that appears as a key within the "bindings"
array will have appeared in the "vars"
array in the results header.
A variable does not appear in an array element if it is not bound in that particular query solution.
The order of elements in the bindings array reflects the order, if any, of the query solution sequence.
"bindings" : [ { "a" : { ... } , "b" : { ... } } , { "a" : { ... } , "b" : { ... } } ]
If the query returns no solutions, an empty array is used.
"bindings" : []
An RDF term (IRI, literal or blank node) is encoded as a JSON object.
All aspects of the RDF term are represented. The JSON object has
a "type"
member and other members depending on the
specific kind of RDF term.
RDF Term | JSON form |
---|---|
IRI I | {"type": "uri", "value": "I"} |
Literal S | {"type": "literal","value": "S"} |
Literal S with language tag L | { "type": "literal", "value": "S", "xml:lang": "L"} |
Literal S with datatype IRI D | { "type": "literal", "value": "S", "datatype": "D"} |
Blank node, label B | {"type": "bnode", "value": "B"} |
The blank node label is scoped to the results object. That is, two blank nodes with the same label in a single SPARQL Results JSON object are the same blank node. This is not an indication of any internal system identifier the SPARQL processor may use. Use of the same label in another SPARQL Results JSON object does not imply it is the same blank node.
The results of a SPARQL ASK
query are serialized as
a boolean value, giving the result of the query evaluation.
The "link"
member has the same format as the SELECT "link"
member.
This section is not normative.
The following JSON is a serialization of the XML document output.srx:
{ "head": { "link": [ "http://www.w3.org/TR/rdf-sparql-XMLres/example.rq" ], "vars": [ "x", "hpage", "name", "mbox", "age", "blurb", "friend" ] }, "results": { "bindings": [ { "x" : { "type": "bnode", "value": "r1" }, "hpage" : { "type": "uri", "value": "http://work.example.org/alice/" }, "name" : { "type": "literal", "value": "Alice" } , "mbox" : { "type": "literal", "value": "" } , "blurb" : { "datatype": "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral", "type": "literal", "value": "<p xmlns=\"http://www.w3.org/1999/xhtml\">My name is <b>alice</b></p>" }, "friend" : { "type": "bnode", "value": "r2" } }, { "x" : { "type": "bnode", "value": "r2" }, "hpage" : { "type": "uri", "value": "http://work.example.org/bob/" }, "name" : { "type": "literal", "value": "Bob", "xml:lang": "en" }, "mbox" : { "type": "uri", "value": "mailto:[email protected]" }, "friend" : { "type": "bnode", "value": "r1" } } ] } }
The Internet Media Type / MIME Type for the SPARQL Query Results JSON Format is "application/sparql-results+json".
It is recommended that SPARQL Query Results JSON Format files have the extension ".srj" (all lowercase) on all platforms.
It is recommended that SPARQL Query Results JSON Format files stored on Macintosh HFS file systems be given a file type of "TEXT".
This information that follows is intended to be submitted to the IESG for review, approval, and registration with IANA.