ConceptioArchiveW3C TR
W3C TRopen access

rdf sparql json res

W3C · w3c_tr
W3C TR · Standards · License: Open Access
Open Source ↗
w3c, standard

Serializing SPARQL Query Results in JSON /**/ code { font-family: monospace; }

div.constraint, div.issue, div.note, div.notice { margin-left: 2em; }

dt.label { display: run-in; }

.diff-chg { background-color: yellow; } .diff-del { background-color: red; text-decoration: line-through;} .diff-add { background-color: lime; }

table { empty-cells: show; }

table caption { font-weight: normal; font-style: italic; text-align: left; margin-bottom: .5em; }

div.issue { color: red; }

div.exampleInner pre { margin-left: 1em; margin-top: 0em; margin-bottom: 0em} div.exampleOuter {border: 4px double gray; margin: 0em; padding: 0em} div.exampleInner { background-color: #d5dee3; border-top-width: 4px; border-top-style: double; border-top-color: #d3d3d3; border-bottom-width: 4px; border-bottom-style: double; border-bottom-color: #d3d3d3; padding: 4px; margin: 0em } div.exampleWrapper { margin: 4px } div.exampleHeader { font-weight: bold; margin: 4px} /**/ Serializing SPARQL Query Results in JSON W3C Working Group Note 18 June 2007 This version: http://www.w3.org/TR/2007/NOTE-rdf-sparql-json-res-20070618/ Latest version: http://www.w3.org/TR/rdf-sparql-json-res/ Previous version: http://www.w3.org/TR/2006/NOTE-rdf-sparql-json-res-20061004/ Editors: Kendall Grant Clark Lee Feigenbaum Elias Torres Copyright W3C ® MIT ERCIM Keio liability trademark document use Abstract SPARQL SELECT ASK JSON Status of this Document 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 This document is a Working Group Note RDF Data Access Working Group W3C Semantic Web Activity Last Call Working Draft of the SPARQL Query Results XML Format ordered distinct JSON [email protected] public archive Publication as a Working Group Note does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress. This document was produced by a group operating under the 5 February 2004 W3C Patent Policy public list of any patent disclosures Essential Claim(s) section 6 of the W3C Patent Policy Table of Contents Document Element Header Results Variable Binding Results Boolean Results Examples Programmatic Utility Appendices Internet Media Type, File Extension and Macintosh File Type References Acknowledgements Serializing SPARQL Query Results with JSON SPARQL variable binding and boolean query results may be serialized in JSON { "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/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/book3" } , "title": { "type": "literal" , "value": "Harry Potter and the Prisoner Of Azkaban" } } , { "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/book1" } , "title": { "type": "literal" , "value": "Harry Potter and the Philosopher's Stone" } } ] } } The JSON serialization of SPARQL variable binding and boolean query results are accomplished according to the following specification. 1. Document Element The Document Element is represented as a JSON Object. XML <?xml version="1.0"> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> ... </sparql> JSON {...} 2. Header The Header is represented as a member (key-value pair) in the document element JSON Object, where the key is a string head vars XML <head>...</head> JSON "head": {"vars": [...]} The vars [SQRXF] vars XML <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#">

<head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/>

<variable name="mbox"/> <variable name="blurb"/> </head> ... </sparql> JSON { "head": { "vars": [ "x", "hpage", "name", "mbox", "age", "blurb", "friend" ] }... The same order constraints described in [SQRXF] vars [SQRXF] [SQRXF] For either boolean or variable binding query results, the head link XML <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#">

<head>

... <link href="metadata.rdf"/> </head> ... </sparql> JSON { "head": { "link": ["http://example.org/metadata.rdf"], ... }... 3. Results The Results are serialized as an object member, the key of which is the string results boolean SELECT ASK results bindings boolean true false XML <results> <result> ... </result>

... </results> JSON {... "results": { "bindings": [...] } } 3.a Variable Binding Results For "each Query Solution in the query results" a Query Solution Object -- that is, a JSON object -- is added to the bindings [SQRXF] [SQRXF] vars The following describes the serialization of RDF Terms in XML and JSON: RDF URI Reference U XML: <binding><uri> U </uri><binding> JSON: "name" : {"type":"uri", "value":" U ""} RDF Literal S XML: <binding><literal> S </literal><binding> JSON: "name" : {"type":"literal", "value":" S "} RDF Literal S L XML: <binding><literal xml:lang=" L "> S </literal><binding> JSON: "name" : {"type":"literal", "xml:lang":" L ", "value":" S "} RDF Typed Literal S D XML: <binding><literal datatype=" D "> S </literal><binding> JSON: "name" : {"type":"typed-literal", "datatype":" D ", "value":" S "} Blank Node label I XML: <binding><bnode> I </bnode><binding> JSON: "name" : {"type":"bnode", "value":" I "} No binding XML: "If, for a particular solution, a variable is unbound binding result [SQRXF] JSON: If, for a particular solution, a variable is unbound bindings 3.b Boolean Results A boolean is serialized as an object member, the key of which is the string "boolean". The value of the object member boolean JSON true false XML <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#">

<head> ... </head> <boolean>true</boolean>

</sparql> JSON { "head": ... "boolean" : true } The vars head link head head null {} 4. Example 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": "typed-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" } } ] } } 5. Programmatic Utility In programming languages with a concise syntax for accessing data structures and their elements, the JSON serialization of SQRXF offers a path-like language for accessing the parts of SPARQL query results. For example, consider the following code fragments: JavaScript // The array of metadata links // returns: http://www.w3.org/TR/rdf-sparql-XMLres/example.rq alert(sr.head.link);

// The first link in the array // returns: http://www.w3.org/TR/rdf-sparql-XMLres/example.rq alert(sr.head.link[0]);

// The bindings array // [object Object],...,[object Object] alert(sr.results.bindings);

// The name of the last variable // returns: friend alert(sr.head.vars.slice(-1));

var bindings = sr.results.bindings;

// JavaScript for...in loop iterates // through the properties of bindings array // which are [0,1,length-1] as opposed to the // array item.

for(i in bindings) { var binding = bindings[i]; alert(binding); // a for-loop to print all the bindings }

// The only difference here (a subtle one) is // that the iterator variable is n as opposed to r // n=name, r=row index for(i in bindings) { var binding = bindings[i]; for(n in binding) { alert(binding[n].value); // a nested for-loop to print binding values } }

alert(bindings[0].hpage.value); A. Internet Media Type, File Extension and Macintosh File Type (Normative) The Internet Media Type / MIME Type for the SPARQL Query Results JSON Format is "application/sparql-results+json". It is recommended that SPARQL query files have the extension ".srj" (all lowercase) on all platforms. It is recommended that SPARQL query 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. Type name: application Subtype name: sparql-results+json Required parameters: None Optional parameters: None Encoding considerations: The encoding considerations of the SPARQL Query Results JSON Format is identical to those of the "application/json" as specified in [JSON-IETF-DRAFT] Security considerations: SPARQL query results uses URIs. See Section 7 of [RFC3986]. Interoperability considerations: There are no known interoperability issues. Published specification: http://www.w3.org/TR/2006/NOTE-rdf-sparql-json-res-20061004/ Applications which use this media type: No known applications currently use this media type. Additional information: Magic number(s): n/a File extension(s): ".srj" Macintosh file type code(s): "TEXT" Person & email address to contact for further information: Kendall Clark, Elias Torres, Lee Feigenbaum <[email protected]> Intended usage: COMMON Restrictions on usage: None Author/Change controller: The SPARQL specification is a work product of the World Wide Web Consortium's RDF Data Access Working Group. The W3C has change control over these specifications. B. References Normative [SQRXF] SPARQL Query Results XML Format Informative [JSON] JavaScript Object Notation, JSON [JSON-IETF-DRAFT] The application/json Media Type for JavaScript Object Notation (JSON), Internet Draft, D. Crockford, http://www.ietf.org/rfc/rfc4627.txt C. Acknowledgements The SPARQL RDF Query Language is a product of the whole of the W3C RDF Data Access Working Group present and past members Changes Since October 2006 Publication Revision 1.10 2007/05/07 17:34:50 lfeigenb removed distinct and ordered attributes to match XML format

Revision 1.9 2006/06/16 17:11:02 etorres *** empty log message ***

Revision 1.8 2006/06/06 14:12:47 lfeigenb changed text/json to application/json and added a cite for the JSON IETF Internet Draft

Revision 1.7 2006/05/17 13:23:28 eric validate for publication

Revision 1.6 2006/04/05 15:55:17 etorres - changed final URL suggestion

Related documents

Record · ID 19485 · SHA-256 2e176c49a496bf66
Conceptio Open Knowledge Archive — every document is proof-bundled with source, license, and retrieval metadata.