GithubHelp home page GithubHelp logo

tpt / sparqljson-parse.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rubensworks/sparqljson-parse.js

0.0 0.0 0.0 322 KB

Parses SPARQL JSON query results

License: MIT License

TypeScript 100.00%

sparqljson-parse.js's Introduction

SPARQL-Results+JSON Parse

Build status Coverage Status npm version

A utility package that allows you to parse SPARQL JSON results in a convenient RDFJS-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" } }
    ]
  }
}

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') },
]

Where namedNode is an RDFJS named node.

This library automatically converts all SPARQL JSON result values to their respective RDFJS 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.

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

sparqljson-parse.js's People

Contributors

rubensworks avatar renovate[bot] avatar greenkeeper[bot] avatar tpt avatar renovate-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.