GithubHelp home page GithubHelp logo

rubensworks / sparqljson-to-tree.js Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 5.0 364 KB

Converts SPARQL JSON results to a tree-based structure

License: MIT License

TypeScript 98.64% JavaScript 1.36%
hacktoberfest

sparqljson-to-tree.js's Introduction

SPARQL-Results+JSON to tree

Build status Coverage Status npm version

A utility package that allows you to convert SPARQL JSON results to a tree-based structure. This is done by splitting variable names by a certain delimiter value (such as _) and using these as paths inside a tree structure.

For example, it can convert the following SPARQL JSON results as follows:

Input:

{
  "results": {
    "bindings": [
      { "books_name": { "type": "literal", "value": "Book 1" }, "books_author_name": { "type": "literal", "value": "Person 1" } },
      { "books_name": { "type": "literal", "value": "Book 2" }, "books_author_name": { "type": "literal", "value": "Person 2" } },
      { "books_name": { "type": "literal", "value": "Book 3" }, "books_author_name": { "type": "literal", "value": "Person 3" } },
      { "books_name": { "type": "literal", "value": "Book 4" }, "books_author_name": { "type": "literal", "value": "Person 4" } },
      { "books_name": { "type": "literal", "value": "Book 5" }, "books_author_name": { "type": "literal", "value": "Person 5" } }
    ]
  }
}

Output:

{
  "books": [
    { "name": "Book 1", "author": { "name": "Person 1" } },
    { "name": "Book 2", "author": { "name": "Person 2" } },
    { "name": "Book 3", "author": { "name": "Person 3" } },
    { "name": "Book 4", "author": { "name": "Person 4" } },
    { "name": "Book 5", "author": { "name": "Person 5" } },
  ]
}

Usage

Create a new converter

import {Converter} from "sparqljson-to-tree";

const converter = new Converter();

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

const converter = new Converter({
  delimiter: '_', // The string to split variable names by. (Default: '_')
  materializeRdfJsTerms: true, // If terms should be converted to their raw value instead of being represented as RDFJS terms (Default: false)
});

Convert using a schema

In order to convert a SPARQL JSON response, we also need to provide a schema that tells which variables need to be seen as singular and which ones as plural.

We do this using the singularizeVariables entry in the schema object. For each variable (and delimited variable part), we can provide entries indicating if the part should be marked as singular. If a variable part is not defined, it will be marked as plurar by default, which is consistent with the open-world-assumption of RDF.

Note: If a variable part is marked as singlular, but multiple bindings apply, then only the first binding will be used.

const sparqlResponse = { results: { bindings: [
  { books_name: { type: 'literal', value: 'Book 1' } },
  { books_name: { type: 'literal', value: 'Book 2' } },
  { books_name: { type: 'literal', value: 'Book 3' } },
  { books_name: { type: 'literal', value: 'Book 4' } },
  { books_name: { type: 'literal', value: 'Book 5' } },
] } };
const schema = { singularizeVariables: {
  '': true, // So we have an object as root instead of an array
  books: false,
  books_name: true,
} };
converter.sparqlJsonResultsToTree(sparqlResponse, schema);

Output:

{
  "books": [
    { "name": "Book 1" },
    { "name": "Book 2" },
    { "name": "Book 3" },
    { "name": "Book 4" },
    { "name": "Book 5" }
  ]
}

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

sparqljson-to-tree.js's People

Contributors

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

Stargazers

Aaron Gray avatar Tom Davidson avatar Sjoerd de Jong avatar Pierce Brooks avatar Yufan Lou avatar jon ⚝ avatar Mike Henrichs avatar Chris Hart avatar Kevin Nolan avatar Angelo Veltens avatar Oleksandr Starnikov avatar Ivan Ogasawara avatar  avatar Patrick Gawron avatar Roy Hiroyuki Osaki avatar Jiri Kuncar avatar ArtificialChat Inc. avatar

Watchers

 avatar James Cloos avatar  avatar

sparqljson-to-tree.js's Issues

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 🌴

Control over whether xsd:dateTime is converted

I was wondering if there was any means keep the conversion of values of datatype xsd:dateTime from being converted creating the TreeConverter to materializeRdfJsTerms to true.

I didn't see anything in the code

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 *
  • rdf-literal ^1.3.2
  • sparqljson-parse ^2.0.0
  • @types/jest ^28.0.0
  • @types/minimist ^1.2.0
  • coveralls ^3.0.0
  • jest ^28.0.0
  • manual-git-changelog ^1.0.0
  • pre-commit ^1.2.2
  • rdf-data-factory ^1.0.2
  • ts-jest ^28.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 ^4.10.0

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

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

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.