⚠️ This repository is archived: It's contents have been moved to a new monorepo.
JSON-LD makes working with RDF easier, but it forces data into a tree structure. JSON 2 Graph Object makes a true graph from Object literals.
JSON-LD is great for making RDF easy for JavaScript developers, but it's not perfect.
{
"@context": {
"friend": { "@type": "@id" }
},
"@id": "http://example.com/friend1",
"friend": {
"@id": "http://example.com/friend2",
"friend": "http://example.com/friend1"
}
}The above example is not a true graph, as the friend field in friend2 is simply the id of the friend, not the friend itself.
npm i jsonld2graphobjectProvide any compliant JSON-LD document as well as the id for the return node.
import{jsonld2graphobject}from"jsonld2graphobject";asyncfunctionrun(){constfriend1=awaitjsonld2graphobject({"@context": {"friend": {"@type": "@id"}},"@id": "http://example.com/friend1","friend": {"@id": "http://example.com/friend2","friend": "http://example.com/friend1"}},"http://example.com/friend1",{excludeContext: false}// excludeContext defaults to false);// Prints friend 1console.log(friend1.friend.friend.friend.friend.friend.friend.friend.friend.friend.friend);}run();- This library doesn't consider named graphs