Re: JSON from SPARQL for multiple assertions

> 
> In the following:
> 
> _:a    ex:name   "Robert" .
> _:a    ex:name   "Bobby" .
> 
> select ?name
> where {?a ex:name ?name}
> 
> should the JSON output have a binding per name assertion or gather 
> the name values in one binding? - one binding is much easier to 
> process. If the values are gathered, should the literals be in one 
> array or an array of values? In other words:
> 
>      "bindings": [
>        {
>          "name": { "type": "literal" , "value": "Robert" }
>        } ,
>        {
>          "name": { "type": "literal" , "value": "Bobby" }
>        } ,

This is the correct serialization as per 
http://www.w3.org/TR/2006/NOTE-rdf-sparql-json-res-20061004/#variable-binding-results 
. The spec says:

"""
For "each Query Solution in the query results" a Query Solution Object -- 
that is, a JSON object -- is added to the bindings array
"""

This is in keeping with the SPARQL Query Results XML Format (see 
http://www.w3.org/TR/rdf-sparql-XMLres/#results ).

If you are working with SPARQL results from JavaScript and are doing 
queries such as this example where you wish to only deal with a simple 
array of result values, you may be interested in the sparql.js JavaScript 
library, which allows you to write code such as:

  var sparqler = new SPARQL.Service("http://sparql.org/sparql");
  ... 
  var names = sparqler.selectValues("SELECT ?name WHERE { ?a ex:name ?name 
}"); // names is a JS array
  for (var i = 0; i < names.length; i++) {
    var name = names[i];
    // do something with name
  }

You can download the sparql.js library at: 
http://www.thefigtrees.net/lee/sw/sparql.js . Some more information on the 
library and some more usage examples are at: 
http://www.thefigtrees.net/lee/blog/2006/04/sparql_calendar_demo_a_sparql.html 
.

Lee


>       OR
> 
>      "bindings": [
>        {
>          "name": { "type": "literal" , "value": ["Bobby", "Robert"] }
>        } ,
>                     OR
> 
>      "bindings": [
>        {
>          "name": [{ "type": "literal" , "value": "Bobby" }, { "type": 
> "literal" , "value": "Robert" }]
>        } ,
> 
> 
> 

Received on Saturday, 7 October 2006 21:02:04 UTC