GithubHelp home page GithubHelp logo

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

sparqljson-parse.js's People

Contributors

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

Stargazers

 avatar Aaron Gray avatar Olivier Filangi avatar Chris Hart avatar Michael Pabst avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

tpt

sparqljson-parse.js's Issues

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 23.10.4 to 23.10.5.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 48 commits.

  • 8cd933b Merge pull request #874 from kulshekhar/release/23.10.5
  • 8742886 chore(release): 23.10.5
  • 1892e1b build(deps-dev): bump @types/babel__core from 7.0.1 to 7.0.2 (#872)
  • 10ff3fd build(deps-dev): bump @types/node from 10.12.0 to 10.12.10 (#883)
  • 0445500 More safely resolve 'jest-config'. (#853)
  • 87ef5b8 Merge pull request #862 from huafu/upgrade-packages
  • 0150319 build(packages): upgrade bs-logger
  • 794b2f2 build(packages): updates e2e test cases dependencies
  • d325bce build(packages): re-lock semver version above wanted
  • 0abd4d4 build(packages): upgrades external dependencies
  • 04a5ebf Fix security vulnerability (#850)
  • 89269d5 Fix wrong value in package.json example (#849)
  • 0a59b42 Merge pull request #848 from orta/patch-1
  • 9162ebb Update README.md
  • 44948c3 build(deps-dev): bump tslint-plugin-prettier from 2.0.0 to 2.0.1 (#832)

There are 48 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 23.10.0 to 23.10.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 13 commits.

  • d9c5b45 Merge pull request #743 from huafu/release/23.10.1
  • e4a3a09 chore(release): 23.10.1
  • ab94359 Merge pull request #742 from huafu/fix-740-no-js-compilation-with-allow-js
  • a844fd4 Merge branch 'master' into fix-740-no-js-compilation-with-allow-js
  • 18dced1 Merge pull request #741 from huafu/e2e-weird-deep-paths
  • 9e7d6a0 test(config): adds test related to allowJs
  • 374dca1 fix(compile): js files were never transpiled thru TS
  • 70fd9af ci(cache): removes some paths from the caching
  • c12dfff fix(windows): normalize paths
  • 0141098 test(e2e): deep paths and coverage
  • 6ccbff3 Merge pull request #736 from huafu/detect-import-and-throw-if-none
  • a2a4be2 fix(config): warn instead of forcing ESM interoperability
  • 21644da Merge pull request #735 from huafu/master

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 23.3.9 to 23.3.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 23.1.4 to 23.10.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for 23.10.0

ts-jest, reloaded!

  • lots of new features including full type-checking and internal cache (see changelog)
  • improved performances
  • Babel not required anymore
  • improved (and growing) documentation
  • a ts-jest Slack community where you can find some instant help
  • end-to-end isolated testing over multiple jest, typescript and babel versions
Commits

The new version differs by 293 commits.

  • 0e5ffed chore(release): 23.10.0
  • 3665609 Merge pull request #734 from huafu/appveyor-optimizations
  • 45d44d1 Merge branch 'master' into appveyor-optimizations
  • 76e2fe5 ci(appveyor): cache npm versions as well
  • 191c464 ci(appveyor): try to improve appveyor's config
  • 0f31b42 Merge pull request #733 from huafu/fix-test-snap
  • 661853a Merge branch 'master' into fix-test-snap
  • aa7458a Merge pull request #731 from kulshekhar/dependabot/npm_and_yarn/tslint-plugin-prettier-2.0.0
  • 70775f1 ci(lint): run lint scripts in series instead of parallel
  • a18e919 style(fix): exclude package.json from tslint rules
  • 011b580 test(config): stop using snapshots for pkg versions
  • 7e5a3a1 build(deps-dev): bump tslint-plugin-prettier from 1.3.0 to 2.0.0
  • fbe90a9 Merge pull request #730 from kulshekhar/dependabot/npm_and_yarn/@types/node-10.10.1
  • a88456e build(deps-dev): bump @types/node from 10.9.4 to 10.10.1
  • 54fd239 Merge pull request #729 from kulshekhar/dependabot/npm_and_yarn/prettier-1.14.3

There are 250 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Buffer is not defined

When used in a browser without Node.js polyfills, the jsonparse library produces an error about Buffer not being defined.

The error comes from here, in the Parser function: https://github.com/creationix/jsonparse/blob/1.3.1/jsonparse.js#L59

But there are also other locations where the global is used.

The library exports the Parser function at the end, which is then imported here and called in two locations, producing the error:

So whenever the Parser function is called, it attempts to access the Buffer global by the looks of it.

Maybe that global could somehow be provided by hand, when there is no Webpack or other means to do it? Or maybe the Buffer could be directly replaced with Uint8Array if it does not use any methods from the Node subclass?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @rdfjs/types *
  • @types/readable-stream ^2.3.13
  • @bergos/jsonparse ^1.4.1
  • rdf-data-factory ^1.1.0
  • readable-stream ^4.0.0
  • @types/jest ^29.0.0
  • @types/minimist ^1.2.0
  • arrayify-stream ^2.0.0
  • coveralls ^3.0.0
  • jest ^29.0.0
  • jest-rdf ^1.7.0
  • manual-git-changelog ^1.0.0
  • pre-commit ^1.2.2
  • streamify-string ^1.0.1
  • ts-jest ^29.0.0
  • ts-loader ^9.3.1
  • tslint ^6.0.0
  • tslint-eslint-rules ^5.3.1
  • typescript ^5.0.0
  • webpack ^5.73.0
  • webpack-cli ^5.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Issues with Webpack 5

Hi,

First of all: thank you for making this package and the whole ecosystem of JS libraries for RDF!

Just wanted to share that using sparqljson-parse with Webpack 5 in client-side code needed a few adjustments in terms of dependencies.

I had to install stream-browserify and buffer and add them in https://webpack.js.org/configuration/resolve/#resolvefallback

        fallback: {
            'stream': require.resolve('stream-browserify'),
            'buffer': require.resolve('buffer'),
        }

A possible way to reproduce the issue is this repository https://github.com/Wanderduene/quickstart-webpack

The issue is not relate so much to the sparqljson-parse package, but the fact that webpack 5 does no longer include a polyfill for this Node.js variables so using the package in front-end code won't work.

An in-range update of @types/node is breaking the build 🚨

The dependency @types/node was updated from 12.12.1 to 12.12.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/node-12.12.2 at 100.0% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.1 to 24.0.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.9 to 24.0.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/jest-24.0.10 at 100.0% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.