Library Version 12.1.3.0

JavaScript Object Notation (JSON) Format Support
Prev | Chapter 5. Query RDF Graphs | Next

## JavaScript Object Notation (JSON) Format Support

JavaScript Object Notation (JSON) format is supported for SPARQL query responses. JSON data format is simple, compact, and well suited for JavaScript programs.

For example, consider the following Java code snippet, which executes a query over the data stored in the Oracle NoSQL Database, and then calls the `ResultSetFormatter.outputAsJSON()` method to present the retrieved results:

    OracleNoSqlConnection conn =
                         OracleNoSqlConnection.createInstance(storeName,
                                                              hostName,
                                                              hostPort);
    OracleGraphNoSql graph = new OracleNamedGraphNoSql(graphName, conn);
    OracleModelNoSql model = new OracleModelNoSql(graph);

    graph.add(new Triple(Node.createURI("http://ds1"),
    Node.createURI("http://dp1"),
    Node.createURI("http://do1")));

    graph.add(new Triple(Node.createURI("http://ds2"),
    Node.createURI("http://dp2"),
    Node.createURI("http://do2")));

    Query q = QueryFactory.create("select ?s ?p ?o where {?s ?p ?o}",
    Syntax.syntaxARQ);

    QueryExecution qexec = QueryExecutionFactory.create(q, model);
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.outputAsJSON(System.out, results);

After the execution of this code, the following JSON output is produced:

    {
    "head": {
    "vars": [ "s" , "p" , "o" ]
    } ,
    "results": {
    "bindings": [
    {
    "s": { "type": "uri" , "value": "http://ds1" } ,
    "p": { "type": "uri" , "value": "http://dp1" } ,
    "o": { "type": "uri" , "value": "http://do1" }
    } ,
    {
    "s": { "type": "uri" , "value": "http://ds2" } ,
    "p": { "type": "uri" , "value": "http://dp2" } ,
    "o": { "type": "uri" , "value": "http://do2" }
    }
    ]
    }
    }

The preceding example can be modified to execute a query over a remote SPARQL endpoint instead of executing it directly against an Oracle NoSQL Database. (If the remote SPARQL endpoint is outside a firewall, then the HTTP Proxy probably needs to be set.)

    Query q = QueryFactory.create("select ?s ?p ?o where {?s ?p ?o}",
    Syntax.syntaxARQ);
    QueryExecution qexec =
                   QueryExecutionFactory.sparqlService(sparqlURL, q);
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.outputAsJSON(System.out, results);

To extend the first example in this section to named graphs, the following code snippet adds two quads to the same dataset, executes a named graph-based SPARQL query, and serializes the query output into JSON format:

    DatasetGraphNoSql dsg = DatasetGraphNoSql.createFrom(graph);
    graph.close();

    dsg.add(new Quad(Node.createURI("http://g1"),
    Node.createURI("http://s1"),
    Node.createURI("http://p1"),
    Node.createURI("http://o1") )
    );

    dsg.add(new Quad(Node.createURI("http://g2"),
                     Node.createURI("http://s2"),
                     Node.createURI("http://p2"),
                     Node.createURI("http://o2") )
            );

    Query q1 = QueryFactory.create(
                     "select ?g ?s ?p ?o where { GRAPH ?g {?s ?p ?o} }");

    QueryExecution qexec1 =
                 QueryExecutionFactory.create(q1, DatasetImpl.wrap(dsg));

    ResultSet results1 = qexec1.execSelect();
    ResultSetFormatter.outputAsJSON(System.out, results1);
    dsg.close();
    conn.dispose();

The JSON output produced after executing the code is presented as follows:

    {
    "head": {
    "vars": [ "g" , "s" , "p" , "o" ]
    } ,
    "results": {
    "bindings": [
    {
    "g": { "type": "uri" , "value": "http://g1" } ,
    "s": { "type": "uri" , "value": "http://s1" } ,
    "p": { "type": "uri" , "value": "http://p1" } ,
    "o": { "type": "uri" , "value": "http://o1" }
    } ,
    {
    "g": { "type": "uri" , "value": "http://g2" } ,
    "s": { "type": "uri" , "value": "http://s2" } ,
    "p": { "type": "uri" , "value": "http://p2" } ,
    "o": { "type": "uri" , "value": "http://o2" }
    }
    ]
    }
    }

You can also get a JSON response through HTTP against a Joseki-based SPARQL endpoint, as in the following example. Normally, when executing a SPARQL query against a SPARQL Web service endpoint, the Accept request-head field is set to be application/sparql-results+xml. For JSON output format, replace the Accept request-head field with application/sparql-results+json.

    http://<hostname>:7001/joseki/oracle-nosql?query=
      <URL_ENCODED_SPARQL_QUERY>&output=json

Prev | Up | Next
Syntax Involving Bnodes (Blank Nodes) | Home | Best Practices