0.1.1 ā€¢ Published 11 months ago

@graviola/json-schema-graph-extract v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

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!