Detalhes do pacote

sparqljson-parse

rubensworks43.2kMIT3.0.0

Parses SPARQL JSON query results

sparql, json, rdfjs, rdf

readme (leia-me)

SPARQL-Results+JSON Parse

Build status Coverage Status npm version

A utility package that allows you to parse SPARQL JSON results in a convenient RDF/JS-based datastructure.

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

In:

{
  "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:

[
  { '?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

import {SparqlJsonParser} from "sparqljson-parse";

const sparqlJsonParser = new SparqlJsonParser();

Optionally, you can provide a settings object to the constructor with optional parameters:

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

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

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

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 inside the head as follows by listening to the variables event:

sparqlJsonParser.parseJsonResultsStream(myStream)
    .on('variables', (variables: RDF.Variable[]) => console.log(variables))
    .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.

Advanced: metadata entries

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

{
  "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.

This code is released under the MIT license.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file.

v3.0.0 - 2025-01-08

BREAKING CHANGES

Added

v2.2.0 - 2023-01-27

Added

Fixed

v2.1.2 - 2022-11-09

Fixed

v2.1.1 - 2022-09-06

Fixed

v2.1.0 - 2022-08-03

Added

v2.0.1 - 2022-07-15

Fixed

v2.0.0 - 2022-07-14

This release has been marked as a major change due to the transition from Node's internal stream API to readable-stream. Most users should experience not breakages with this change.

Changed

v1.7.0 - 2021-08-11

Changed

v1.6.1 - 2021-07-27

Fixed

v1.6.0 - 2020-09-16

Changed

v1.5.2 - 2020-04-28

Changed

v1.5.1 - 2019-08-22

Fixed

v1.5.0 - 2018-11-08

Changed

1.3.0 - 2018-10-02

Added

1.3.0 - 2018-09-05

Fixed

1.3.0 - 2018-08-21

Added

[1.2.0] - 2018-08-06