@graviola/json-schema-graph-extract
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

SPARQL Query generator: @graviola/json-schema-graph-extract

Version Documentation License: MIT

A library to get JSON documents using JSON-Schema out of a knowledge graph

🏠 Homepage

Prerequisites

  • node >=10

Install

yarn add @graviola/json-schema-graph-extract

Usage

given the following json schema

{
  "$defs": {
    "Address": {
      "type": "object",
      "properties": {
        "addressCountry": {
          "type": "string"
        },
        "addressRegion": {
          "type": "string"
        },
        "postalCode": {
          "type": "string"
        },
        "streetAddress": {
          "type": "string"
        },
      }
    },
    "Person": {
      "title": "Person",
      "description": "A human being",
      "type": "object",
      "properties": {
        "familyName": {
          "type": "string"
        },
        "givenName": {
          "type": "string"
        },
        "address": {
          "$ref": "#/$defs/Address"
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$id": "https://example.com/person.schema.json",
}

and a knowledge graph that contains the following triples:

@prefix : <https://example.com/person.schema.json#> .
@prefix ex: <http://example.com/entities#> .

ex:me a :Person ;
  :familyName "Meneman" ;
  :givenName "Max" ;
  :address ex:address .
    :addressCountry "Germany" ;
    :addressRegion "Berlin" ;
    :postalCode "10967" ;
    :streetAddress "Humboldforum 7821" .

call the extractDocumentFromDataset function with on any dataset:

    const baseIRI = 'http://example.com',
          entityIRI = 'http://example.com/entities#me'
    const data = extractDocumentFromDataset(
      baseIRI, 
      entityIRI, 
      dataset, 
      schema, 
      { 
        omitEmptyArrays: true,
        omitEmptyObjects: true,
        maxRecursionEachRef: 1,
        maxRecursion: 5 
      })

the result data serialized to json will look like this:

{
  "@id": "http://example.com/entities#me",
  "@type": "Person",
  "familyName": "Meneman",
  "givenName": "Max",
  "address": {
    "addressCountry": "Germany",
    "addressRegion": "Berlin",
    "postalCode": "10967",
    "streetAddress": "Humboldforum 7821"
  }
}

if the entry exists within the dataset and the document is valid according to the schema.

In the example the maxRecursionEachRef and maxRecursion options are used to limit the depth of the recursion. It is possible to have a schema that references itself, which would lead to an infinite recursion.

In future versions a more fine grained control over the recursion will be possible.

The omitEmptyArrays and omitEmptyObjects options are used to omit empty arrays and objects from the result.

Author

👤 Sebastian Tilsch

Show your support

Give a ⭐️ if this project helped you!

/@graviola/json-schema-graph-extract/

    Package Sidebar

    Install

    npm i @graviola/json-schema-graph-extract

    Weekly Downloads

    0

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    180 kB

    Total Files

    18

    Last publish

    Collaborators

    • graviola