Giter VIP home page Giter VIP logo

ttl2jsonld's Introduction

@frogcat/ttl2jsonld

Turtle to JSON-LD converter for node.js and browser, no library dependencies.

Demo

Online demo is available here.

With this converter, you can obtain Output JSON-LD from Input Turtle, as shown in JSON-LD 1.0 Specification .

Input Turtle

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .

Output JSON-LD

{
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/"
  },
  "@id": "http://manu.sporny.org/about#manu",
  "@type": "foaf:Person",
  "foaf:name": "Manu Sporny",
  "foaf:homepage": { "@id": "http://manu.sporny.org/" }
}

Install

node.js

$ npm install @frogcat/ttl2jsonld

browser

<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>

Usage

node.js

You can write your own code.

const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;

const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld(ttl);

console.log(JSON.stringify(jsonld,null,2));

Command line interface is also available.

$ npm install @frogcat/ttl2jsonld
$ ttl2jsonld {input_turtle} > {output_jsonld}
or
$ cat {input_turtle} | ttl2jsonld > {output_jsonld}

browser

This converter is exported to ttl2jsonld global object. Call ttl2jsonld.parse(ttl) to perform conversion.

<!-- include ttl2jsonld.js -->
<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>

<!-- run script -->
<script>
const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld.parse(ttl);

console.log(JSON.stringify(jsonld,null,2));
</script>

API

Given turtle_string, ttl2jsonld.parse returns JSON Object.

var json_object = ttl2jsonld.parse(turtle_string);

When you want to pass baseIRI, give second argument like this.

var json_object = ttl2jsonld.parse(turtle_string, {
  baseIRI : "http://example.org/"  
});

For developers

building

  • Build with PEG.js.
  • Main code is spec/ttl2jsonld.pegjs.
  • When you edit code, run npm run build to generate ttl2jsonld.js.

testing

  • Tested with rdf-test-suite.
  • Currently 294 / 298 tests succeeded.
  • Run npm run spec-turtle to start rdr-test-suite.
  • Other miscellaneous tests are located in test/test.js, npm run test to run.

ttl2jsonld's People

Contributors

frogcat avatar your1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ttl2jsonld's Issues

edge prefix is making bug the ttl2jsonld

If you have the following prefix:
@Prefix : https://schema.mekanoid.io/mekanoid.owl# .

:defender_machine_321ddbxxxxxxxxxxxxxx1776bbd3918b7a8 rdf:type d3f:Host ;
:comment "Agent version 10.8210.17763.3650", "Risk Score None", "Status Inactive" ;

it creates keys in json-ld like "0:defender_machine__321ddbxxxxxxxxxxxxxx1776bbd3918b7a8" or "0:comment" and not ":defender_machine__321ddbxxxxxxxxxxxxxx1776bbd3918b7a8" or ":comment" like it should.

@id value must be an absolut IRI

I encountered this issue when implementing ttl2jsonld to serialize jsonld in https://github.com/linkeddata/rdflib.js (see screens below)
Your app does a good job to create a compact jsonld output with relative links from a compact ttl.
rdflib produce compact ttl with relative links and collections @list.

Actually the only issue is when ttl contains @prefix with non abolute IRI, like @prefix : <#>. or @prefix n: </#>.
@id value accepts relative url without any problem.

I made a simple function to find non valid IRI in @context. Then rebuild any @id value and delete @context declaration.
A better solution would to make a PR.

  statementsToJsonld (sts) {
    // ttl2jsonld creates context keys for all ttl prefix
    // context keys must be full IRI
    function findId (itemObj) {
      if (itemObj['@id']) {
        const item = itemObj['@id'].split(':')
        if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1]
      }
      const itemValues = Object.values(itemObj)
      for (const i in itemValues) {
        if (typeof itemValues[i] !== 'string') { // @list contains array
          findId(itemValues[i])
        }
      }
    }

    const turtleDoc = this.statementsToN3(sts)
    const jsonldObj = ttl2jsonld.parse(turtleDoc)
    const context = jsonldObj['@context']
    const iri = /^[a-z](.*?):(.+?)/g // begin with a letter, contain : followed by at least one character
    const keys = Object.keys(context).filter(key => !context[key].match(iri))

    findId(jsonldObj)
    keys.map(key => { delete jsonldObj['@context'][key] })
    console.log(JSON.stringify(jsonldObj, null, 2))

    return JSON.stringify(jsonldObj, null, 2)
  }

image

image

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.