Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
rdf-object
Advanced tools
RDF Object makes it easier to read RDF data by loading it as JSON objects.
This library accepts RDFJS-compliant quads.
$ yarn install rdf-object
This package also works out-of-the-box in browsers via tools such as webpack and browserify.
import {RdfObjectLoader} from "rdf-object";
or
const RdfObjectLoader = require("rdf-object").RdfObjectLoader;
RdfObjectLoader
accepts RDF quad/triple streams (or arrays) as input,
and loads the resulting graph in-memory as linked Resource
s.
A Resource
is a wrapper over an RDFJS term that holds property links.
Using a JSON-LD context,
properties are easily accessible.
The following examples assume the following imports:
import {namedNode, literal, triple} as DataFactory from "@rdfjs/data-model"; // External library
import {RdfObjectLoader} from "rdf-object";
RdfObjectLoader
accepts a JSON-LD context as input argument.
This context can also be the URL to a JSON-LD context.
// Initialize our loader with a JSON-LD context
const context = {
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'type': 'rdf:type',
'label': 'rdfs:label',
'foaf': 'http://xmlns.com/foaf/0.1/',
'knows': 'foaf:knows',
'name': 'foaf:name',
'ex': 'http://example.org/'
};
const myLoader = new RdfObjectLoader({ context });
Resources are stored inside the resources
field of RdfObjectLoader
,
they are indexed by URI.
Each resource has the following fields:
value
: The term value. For example a URI or literal value.type
: The RDFJS term type.term
: The RDFJS term.// Import triples
myLoader.importArray([
triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
]).then(() => {
// Get property values by shortcut
const myResource = myLoader.resources['http://example.org/myResource'];
console.log(`URI: ${myResource.value}`);
console.log(`Term type: ${myResource.type}`);
console.log(`Term value: ${myResource.value}`);
console.log(`Term: ${myResource.term}`);
});
Alternatively, myLoader.import()
can be invoked on
an RDFJS stream of triples/quads.
This can for example accept parsed turtle streams.
Multiple calls to importArray
and import
can be done at any time
to easily combined multiple sources.
The property
field on a Resource
contains all property values.
It maps all predicates to objects,
where each predicate is a URI or JSON-LD shortcut,
and each object is a Resource
.
If multiple values are present for that property, only the first will be returned.
// Import triples
myLoader.importArray([
triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
]).then(() => {
// Get property values by shortcut
const myResource = myLoader.resources['http://example.org/myResource'];
console.log(`URI: ${myResource}`);
console.log(`Type: ${myResource.property.type}`);
console.log(`Label: ${myResource.property['rdfs:label']}`);
console.log(`Label (bis): ${myResource.property['http://www.w3.org/1999/02/22-rdf-syntax-ns#label']}`);
});
Via the properties
field on Resource
,
all values of a property can be retrieved.
JSON-LD can also be used on properties
.
// Import triples
myLoader.importArray([
triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
]).then(() => {
// Get property values by shortcut
const myResource = myLoader.resources['http://example.org/myResource'];
console.log(`Labels: ${myResource.properties.label}`);
});
The raw properties (without JSON-LD shortcuts) are
available in the propertiesUri
field on Resource
.
As Resource
properties map to other Resource
s,
nested property paths can be followed easily.
// Import triples
myLoader.importArray([
triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://ruben.verborgh.org/profile/#me')),
triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://data.verborgh.org/people/joachim_van_herwegen')),
triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Taelman')),
triple(namedNode('https://ruben.verborgh.org/profile/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Verborgh')),
triple(namedNode('https://data.verborgh.org/people/joachim_van_herwegen'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Joachim Van Herwegen')),
]).then(() => {
// Get friend names via nested resources
const rubenT = myLoader.resources['https://www.rubensworks.net/#me'];
console.log(`Friends of ${rubenT.property.name}:`);
for (const friend of rubenT.properties.friends) {
console.log(`* ${friend.property.name}`);
}
});
RDF lists are automatically parsed
and exposed as a JavaScript array via the
list
field on Resource
.
// Import triples
myLoader.importArray([
triple(namedNode('http://example.org/myResource'), namedNode('http://example.org/list'), namedNode('http://example.org/myList0')),
triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('A')),
triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList1')),
triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('B')),
triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList2')),
triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('C')),
triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil')),
]).then(() => {
// Get friend names via nested resources
const myResource = myLoader.resources['http://example.org/myResource'];
console.log(`List values of ${myResource}`);
for (const listElement of myResource.property['ex:list'].list) {
console.log(`* ${listElement}`);
}
});
If you don't want RDF lists to be parsed automatically,
you can set normalizeLists
to false
in the RdfObjectLoader
constructor.
This software is written by Ruben Taelman.
This code is released under the MIT license.
FAQs
Loads RDF as JSON objects
We found that rdf-object demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.