# sparqljson-parse

> Parses SPARQL JSON query results

Latest version **3.3.0** (published 2025-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install sparqljson-parse
pnpm add sparqljson-parse
yarn add sparqljson-parse
bun add sparqljson-parse
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.3.0 |
| Published | 2025-10-24 |
| First published | 2018-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 39.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Ruben Taelman |
| Maintainers | rubensworks |
| Keywords | sparql, json, rdfjs, rdf, linked data |

## Links

- npm: https://www.npmjs.com/package/sparqljson-parse
- Repository: https://github.com/rubensworks/sparqljson-parse.js
- Homepage: https://github.com/rubensworks/sparqljson-parse.js#readme
- Issues: https://github.com/rubensworks/sparqljson-parse.js/issues
- Funding: https://github.com/sponsors/rubensworks/
- npm.io page: https://npm.io/package/sparqljson-parse

## Dependencies (4)

- [readable-stream](https://npm.io/package/readable-stream.md) ^4.0.0
- [rdf-data-factory](https://npm.io/package/rdf-data-factory.md) ^2.0.0
- [@bergos/jsonparse](https://npm.io/package/@bergos/jsonparse.md) ^1.4.1
- [@types/readable-stream](https://npm.io/package/@types/readable-stream.md) ^4.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.3.0 (latest) — 2025-10-24
- 3.2.0 — 2025-10-23
- 3.1.0 — 2025-06-17
- 3.0.0 — 2025-01-08
- 2.2.0 — 2023-01-27
- 2.1.2 — 2022-11-09
- 2.1.1 — 2022-09-06
- 2.1.0 — 2022-08-03
- 2.0.1 — 2022-07-15
- 2.0.0 — 2022-07-14
- 1.7.0 — 2021-08-11
- 1.6.1 — 2021-07-27
- 1.6.0 — 2020-09-16
- 1.5.2 — 2020-04-28
- 1.5.1 — 2019-08-22
- … 8 more at https://npm.io/package/sparqljson-parse/versions

## README

# SPARQL-Results+JSON Parse

[![Build status](https://github.com/rubensworks/sparqljson-parse.js/workflows/CI/badge.svg)](https://github.com/rubensworks/sparqljson-parse.js/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/sparqljson-parse.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/sparqljson-parse.js?branch=master)
[![npm version](https://badge.fury.io/js/sparqljson-parse.svg)](https://www.npmjs.com/package/sparqljson-parse)

A utility package that allows you to parse [SPARQL JSON](https://www.w3.org/TR/sparql11-results-json/) results
in a convenient [RDF/JS](https://rdf.js.org/)-based datastructure.

For example, the following SPARQL JSON result can be converted as follows:

In:
```json
{
  "head": {
    "vars": [
      "book"
      ]
  },
  "results": {
    "bindings": [
      { "book": { "type": "uri", "value": "http://example.org/book/book1" } },
      { "book": { "type": "uri", "value": "http://example.org/book/book2" } },
      { "book": { "type": "uri", "value": "http://example.org/book/book3" } },
      { "book": { "type": "uri", "value": "http://example.org/book/book4" } },
      { "book": { "type": "uri", "value": "http://example.org/book/book5" } },
      {
        "book": {
          "type": "triple",
          "value": {
            "subject": {
              "type": "uri",
              "value": "http://example.org/alice"
            },
            "predicate": {
              "type": "uri",
              "value": "http://example.org/name"
            }
          }
        }
      }
    ]
  }
}
```

Out:
```javascript
[
  { '?book': namedNode('http://example.org/book/book1') },
  { '?book': namedNode('http://example.org/book/book2') },
  { '?book': namedNode('http://example.org/book/book3') },
  { '?book': namedNode('http://example.org/book/book4') },
  { '?book': namedNode('http://example.org/book/book5') },
    { '?book': quad(namedNode('http://example.org/bob'), namedNode('http://example.org/name'), literal('Bob', namedNode('http://example.org/Type'))) },
]
```

Where `namedNode` is an RDF/JS named node, `quad` is an RDF/JS quad/triple, and `literal` is an RDF/JS literal.

This library automatically converts all SPARQL JSON result values to their respective RDF/JS type.

## Usage

### Create a new parser

```javascript
import {SparqlJsonParser} from "sparqljson-parse";

const sparqlJsonParser = new SparqlJsonParser();
```

Optionally, you can provide a settings object to the constructor with optional parameters:
```javascript
const sparqlJsonParser = new SparqlJsonParser({
  dataFactory: dataFactory, // A custom RDFJS datafactory
  prefixVariableQuestionMark: true, // If variable names in the output should be prefixed with '?', default is false.
});
```

### Convert single bindings

```javascript
sparqlJsonParser.parseJsonBindings({ "book": { "type": "uri", "value": "http://example.org/book/book1" } })
// This will output { '?book': namedNode('http://example.org/book/book1') }
```

### Convert a full SPARQL JSON response

```javascript
const sparqlJsonresponse = {
                             "head": {
                               "vars": [
                                 "book"
                                 ]
                             },
                             "results": {
                               "bindings": [
                                 { "book": { "type": "uri", "value": "http://example.org/book/book1" } }
                               ]
                             }
                           };
sparqlJsonParser.parseJsonResults(sparqlJsonresponse);
// This will output [ { '?book': namedNode('http://example.org/book/book1') } ]
```

### Convert a full SPARQL JSON boolean response

```javascript
const sparqlJsonresponse = {
                             "head": {},
                             "boolean": true
                           };
sparqlJsonParser.parseJsonBoolean(sparqlJsonresponse);
// This will output true
```

### Convert a SPARQL JSON stream

If you have many query results, then a streaming-based approach might be more efficient.
In this case, you can use the `sparqlJsonParser.parseJsonResultsStream` method,
which takes a Node readable stream of SPARQL JSON results as a text stream,
and outputs a stream of parsed bindings.

Optionally, you can also retrieve the variables, links, and version inside the `head`
as follows by listening to the `'variables'`, `'link'`, and `'version'` events:
```
sparqlJsonParser.parseJsonResultsStream(myStream)
    .on('variables', (variables: RDF.Variable[]) => console.log(variables))
    .on('link', (links: string[]) => console.log(links))
    .on('version', (version: string) => console.log(version))
    .on('data', (bindings: IBindings) => console.log(bindings));
```

`sparqlJsonParser.parseJsonBooleanStream` also takes a stream as input,
but it returns a promise that resolves to a boolean.

The error thrown for unsupported versions can be skipped
by setting `parseUnsupportedVersions` to `true` when constructing the parser.

### Advanced: metadata entries

This library can recognise metadata on the result stream in the following form:

```json
{
  "head": { "vars": [ "book", "library" ] },
  "results": {
    "bindings": [
      { "book": { "type": "uri", "value": "http://example.org/book/book1" }, "library": { "type": "uri", "value": "http://example.org/book/library1" } }
    ]
  },
  "metadata": { "httpRequests": 0 }
}
```

This metadata can be captured by listening to the `"metadata"` event:
```
sparqlJsonParser.parseJsonResultsStream(myStream)
    .on('metadata', (metadata: any) => console.log(metadata))
    .on('data', (bindings: IBindings) => console.log(bindings));
```

Note that this is not part of the SPARQL/JSON specification.

## License
This software is written by [Ruben Taelman](http://rubensworks.net/).

This code is released under the [MIT license](http://opensource.org/licenses/MIT).

---
_Source: https://npm.io/package/sparqljson-parse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
