A parser for the SPARQL query language in JavaScript

License: Other

Language: Yacc

Keywords: parser, rdf, serializer, sparql

SPARQL.js – A SPARQL 1.1 parser for JavaScript

Build Status npm version DOI

The SPARQL 1.1 Query Language allows to query datasources of RDF triples. SPARQL.js translates SPARQL into JSON and back, so you can parse and build SPARQL queries in your JavaScript applications.

It fully supports the SPARQL 1.1 specification, including property paths, federation, and updates.



// Parse a SPARQL query to a JSON object
var SparqlParser = require('sparqljs').Parser;
var parser = new SparqlParser();
var parsedQuery = parser.parse(
  'PREFIX foaf: <> ' +
  'SELECT * { ?mickey foaf:name "Mickey Mouse"@en; foaf:knows ?other. }');

// Regenerate a SPARQL query from a JSON object
var SparqlGenerator = require('sparqljs').Generator;
var generator = new SparqlGenerator();
parsedQuery.variables = ['?mickey'];
var generatedQuery = generator.stringify(parsedQuery);


$ sparql-to-json query.sparql


Through browserify:

$ cd SPARQL.js
$ npm install
$ browserify sparql.js -s sparqljs -i fs -i path -i _process > sparqljs-browser.js
$ uglifyjs sparqljs-browser.js -c -m > sparqljs-browser-min.js


Queries are represented in a JSON structure. The most easy way to get acquainted with this structure is to try the examples in the queries folder through sparql-to-json. All examples of the SPARQL 1.1 specification have been included, in case you wonder how a specific syntactical construct is represented.

Here is a simple query in SPARQL:

PREFIX dbpedia-owl: <>
    ?p a dbpedia-owl:Artist.
    ?p dbpedia-owl:birthPlace ?c.
    ?c <> "York"@en.

And here is the same query in JSON:

  "type": "query",
  "prefixes": {
    "dbpedia-owl": ""
  "queryType": "SELECT",
  "variables": [ "?p", "?c" ],
  "where": [
      "type": "bgp",
      "triples": [
          "subject": "?p",
          "predicate": "",
          "object": ""
          "subject": "?p",
          "predicate": "",
          "object": "?c"
          "subject": "?c",
          "predicate": "",
          "object": "\"York\"@en"

The representation of triples is the same as that of the N3.js library.


$ [sudo] npm [-g] install sparqljs

License, status and contributions

The SPARQL.js library is copyrighted by Ruben Verborgh and released under the MIT License.

Contributions are welcome, and bug reports or pull requests are always helpful.


Project Statistics

Sourcerank 10
Repository Size 332 KB
Stars 179
Forks 34
Watchers 25
Open issues 5
Dependencies 72
Contributors 11
Tags 22
Last updated
Last pushed

Top Contributors See all

Ruben Verborgh Alexey Morozov Joachim Van Herwegen Tim Ermilov Artem Kozlov Johannes Trame Benjamin Forster Ruben Taelman Shad Reynolds James Michael DuPont Edgar Rodriguez-Diaz

Packages Referencing this Repo

A parser for the SPARQL query language
Latest release 2.1.0 - Updated - 179 stars

Recent Tags See all

v2.1.0 August 14, 2018
v2.0.3 February 13, 2018
v2.0.2 January 11, 2018
v2.0.1 January 11, 2018
v2.0.0 December 03, 2017
v1.5.2 September 28, 2017
v1.5.1 May 18, 2017
v1.5.0 May 11, 2017
v1.4.1 April 20, 2017
v1.4.0 April 15, 2017
v1.3.0 December 04, 2016
v1.2.0 November 15, 2016
v1.1.4 January 26, 2016
v1.1.3 September 29, 2015
v1.1.2 June 28, 2015

Interesting Forks See all

A parser for the SPARQL query language in JavaScript
JavaScript - Last pushed - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2018-08-14 20:46:44 UTC

Login to resync this repository