Search code examples
jsonsparqlrdf

How to view SPARQL query result as JSON objects?


I want to view SPARQL query results as JSON objects. For example, I have a RDF database where I have parents graph which includes children, relatives and their information. Is there a way to view them as JSON objects like

"Parents": {
  "names": "some names"
   "children":[
     {"child1": {
       "name": name
     }}
   ]

.....

}

How can I achieve this? All suggestions are welcome. Thanks


Solution

  • SPARQL provides "application/sparql-results+json" as its document content-type for query solutions for consumption by applications that understand JSON.

    In recent times I've come to realize that this aspect of SPARQL is generally understood thereby creating artificial friction for "Web Developers" who work with tools that support JSON as the default content-type for structured data.

    Anyway, we recently released a HTML5, CSS, and Javascript based Single Page Application that demonstrates what's possible with SPARQL when you put its "application/results+json" query solution content-type to use. Naturally, it also provides a solution for understanding how to process JSON returned from SPARQL.

    How the form works.

    enter image description here

    Code Snippet regarding JSON object handling

    /*
                        Dynamic Table for processing JSON Structured Data (via "application/sparql-results+json" document content type)
                        that enables INSERT to be handled via a 3-tuple subject, predicate, object graph (relation) while query results 
                        are handled via an N-Tuple structured table (relation).
                        */
                        if (data.results.bindings.length > 0){
                            var table = tabCheckTable("dbmsTableID", "fsTableID") ; // creates table for header
                            var header = table.createTHead(); // creates empty tHead
                            var headRow = header.insertRow(0); // inserts row into tHead
                            var bindings = data.results.bindings;
                            for (var col = 0; col < data.head.vars.length; col++) { // for each column
                                // console.log("col = " + col)
                                var headCell = headRow.insertCell(col); // inserts new cell at position i in thead
                                headCell.innerHTML = "<b>" + data.head.vars[col] + "</b>"; // adds bold text to thead cell
                                }
                            for (i in bindings) {
                                // console.log("i = " + i)
                                var curr = 0 ; // curr is used to keep track of correct cell position
                                var binding = bindings[i];
                                var bodyRow = table.insertRow(-1); // create new row
                                for (n in binding) {
                                //  console.log("n = " + n)
                                //  console.log("curr = " + curr)
                                    var bodyCell = bodyRow.insertCell(curr); // create new cell in row
                                    bodyCell.innerHTML = tableFormat(binding[n].value); // set value of cell
                                    curr += 1 ;
                                }
                            }
                        }   
                        else{
                            throw new Error("No Data Returned");
                            console.log("No data returned by query");
                        }
                    })
                } catch(e) {
                    console.error('Query Failed:', e) ;
                }          
    
            }
    

    Links