Turtle to JSON-LD converter for node.js and browser, no library dependencies.
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 .
@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/> .
{
"@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/" }
}
$ npm install @frogcat/ttl2jsonld
<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>
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}
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>
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/"
});
- Build with PEG.js.
- Main code is
spec/ttl2jsonld.pegjs
. - When you edit code, run
npm run build
to generatettl2jsonld.js
.
- 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.