Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sparqljson-to-tree

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sparqljson-to-tree

Converts SPARQL JSON results to a tree-based structure

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.7K
decreased by-31.95%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 09 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc