Skip to content

LDO for Raw RDF Guide

LDO (Linked Data Objects) is a library that lets you easily manipulate RDF as if it were a standard TypeScript object that follows a ShEx shape you define.

This tutorial will walk you through using LDO on raw RDF. How that raw RDF is fetched is left up the developer.

Completed Code on Github


Automatic Setup

To setup LDO, cd into your typescript project and run npx @ldo/cli init.

cd my-typescript-project
npx @ldo/cli init
Manual Setup The following is handled by the automatic setup: Install the LDO dependencies.
npm install @ldo/ldo
npm install @ldo/cli --save-dev
Create a folder to store your ShEx shapes:
mkdir shapes
Create a script to build ShEx shapes and convert them into Linked Data Objects. You can put this script in `package.json`
  scripts: {
    "build:ldo": "ldo build --input ./shapes --output ./.ldo"

Creating ShEx Schemas

LDO uses ShEx as a schema for the RDF data in your project. To add a ShEx schema to your project, simply create a file ending in .shex to the shapes folder.

For more information on writing ShEx schemas see the ShEx Primer.


PREFIX ex: <>
PREFIX foaf: <>
PREFIX rdfs: <>
PREFIX xsd: <>

ex:FoafProfile EXTRA a {
  a [ foaf:Person ]
    // rdfs:comment  "Defines the node as a Person (from foaf)" ;
  foaf:name xsd:string ?
    // rdfs:comment  "Define a person's name." ;
  foaf:img xsd:string ?
    // rdfs:comment  "Photo link but in string form" ;
  foaf:knows @ex:FoafProfile *
    // rdfs:comment  "A list of WebIds for all the people this user knows." ;

To build the shape, run:

npm run build:ldo

This will generate five files: - ./.ldo/foafProfile.shapeTypes.ts <-- This is the important file - ./.ldo/foafProfile.typings.ts - ./.ldo/foafProfile.schema.ts - ./.ldo/foafProfile.context.ts

Simple Example

Below is a simple example of LDO in a real use-case (changing the name on a Solid Pod)

import { parseRdf, startTransaction, toSparqlUpdate, toTurtle } from "@ldo/ldo";
import { FoafProfileShapeType } from "./.ldo/foafProfile.shapeTypes";

async function run() {
  const rawTurtle = `
  <#me> a <>;
      <> "Jane Doe".

   * Step 1: Convert Raw RDF into a Linked Data Object
  const ldoDataset = await parseRdf(rawTurtle, {
    baseIRI: "",
  // Create a linked data object by telling the dataset the type and subject of
  // the object
  const janeProfile = ldoDataset
    // Tells the LDO dataset that we're looking for a FoafProfile
    // Says the subject of the FoafProfile

   * Step 2: Manipulate the Linked Data Object
  // Logs "Jane Doe"
  // Logs "Person"
  // Logs 0

  // Begins a transaction that tracks your changes
  startTransaction(janeProfile); = "Jane Smith";
    "@id": "",
    type: {
      "@id": "Person",
    name: "John Smith",
    knows: [janeProfile],

  // Logs "Jane Smith"
  // Logs "John Smith"
  // Logs "Jane Smith"

   * Step 3: Convert it back to RDF
  // Logs:
  // <> a <>;
  //   <> "Jane Smith";
  //   <> <>.
  // <> a <>;
  //   <> "John Smith";
  //   <> <>.
  console.log(await toTurtle(janeProfile));
  // Logs:
  //   <> <> "Jane Doe" .
  // };
  //   <> <> "Jane Smith" .
  //   <> <> <> .
  //   <> <> "John Smith" .
  //   <> <> <> .
  //   <> <> <> .
  // }
  console.log(await toSparqlUpdate(janeProfile));

Getting an LDO Dataset

An LDO Dataset is a kind of RDF JS Dataset that can create linked data objects.

LDO datasets can be created in two ways:

createLdoDataset(initialDataset?: Dataset<Quad, Quad> | Quad[])

import { createLdoDataset } from "@ldo/ldo";

const ldoDataset = createLdoDataset();

  • initialDataset: An optional dataset or array of quads for the new dataset.

parseRdf(data: string, parserOptions?: ParserOptions)

import { parseRdf } from "@ldo/ldo";

const rawTurtle = "...";
const ldoDataset =  await parseRdf(rawTurtle, { baseIRI: "" });

  • data: The raw data to parse as a string.
  • options (optional): Parse options containing the following keys:
    • format (optional): The format the data is in. The following are acceptable formats: Turtle, TriG, N-Triples, N-Quads, N3, Notation3.
    • baseIRI (optional): If this data is hosted at a specific location, you can provide the baseIRI of that location.
    • blankNodePrefix (optional): If blank nodes should have a prefix, that should be provided here.
    • factory (optional): a RDF Data Factory from @rdfjs/data-model.

Getting a Linked Data Object

Once you have an LdoDataset we can get a Linked Data Object. A linked data object feels just like a JavaScript object literal, but when you make modifications to it, it will affect the underlying LdoDataset.

Thie first step is defining which Shape Type you want to retrieve from the dataset. We can use the generated shape types and the usingType() method for this.

import { FoafProfileShapeType } from "./.ldo/foafProfile.shapeTypes.ts";

// ... Get the LdoDataset


Next, we want to identify exactly what part of the dataset we want to extract. We can do this in a few ways:


fromSubject lets you define a an entryNode, the place of entry for the graph. The object returned by jsonldDatasetProxy will represent the given node. This parameter accepts both namedNodes and blankNodes. fromSubject takes a generic type representing the typescript type of the given subject.

const profile = ldoDataset

.matchSubject(predicate?, object?, graph?)

matchSubject returns a Jsonld Dataset Proxy representing all subjects in the dataset matching the given predicate, object, and graph.

const profiles = ldoDataset
profiles.forEach((person) => {

.matchObject(subject?, predicate?, object?)

matchObject returns a Jsonld Dataset Proxy representing all objects in the dataset matching the given subject, predicate, and graph.

const friendsOfPerson1 = ldoDataset
friendsOfPerson1.forEach((person) => {


fromJson will take any regular Json, add the information to the dataset, and return a Jsonld Dataset Proxy representing the given data.

const person2 = ldoDataset
    "@id": "",
    fn: ["Jane Doe"],

Getting and Setting Data on a Linked Data Object

Once you've created a Linked Data Object, you can get and set data as if it were a normal TypeScript Object. For specific details, see the documentation at JSONLD Dataset Proxy.

import { LinkedDataObject } from "@ldo/ldo";
import { FoafProfileFactory } from "./.ldo/foafProfile.ldoFactory.ts";
import { FoafProfile } from "./.ldo/foafProfile.typings";

aysnc function start() {
  const profile: FoafProfile = // Create LDO
  // Logs "Aang"
  // Logs "Person"
  // Logs 1
  // Logs "Katara"
  console.log(profile.knows?.[0].name); = "Bonzu Pippinpaddleopsicopolis III"
  // Logs "Bonzu Pippinpaddleopsicopolis III"
    type: { "@id": "Person" },
    name: "Sokka"
  // Logs 2
  // Logs "Katara" and "Sokka"
  profile.knows?.forEach((person) => console.log(;

Converting a Linked Data Object back to RDF

A linked data object can be converted into RDF in multiple ways:


import { toTurtle } from "@ldo/ldo"
// ...
const rawTurtle: string = await toTurtle(profile);


import { toNTriples } from "@ldo/ldo"
// ...
const rawNTriples: string = await toNTriples(profile);

serialize(linkedDataObject, options)

import { serialize } from "@ldo/ldo";

const rawTurtle: string = await serialize({
  format: "Turtle",
  prefixes: {
    ex: "",
    foaf: "",
serialize(linkedDataObject, options) provides general serialization based on provided options: - foramt (optional): The format to serialize to. The following are acceptable formats: Turtle, TriG, N-Triples, N-Quads, N3, Notation3. - prefixes: The prefixes for those serializations that use prefixes.


Sometimes, you want to keep track of changes you make for the object. This is where transactions come in handy.

To start a transaction, use the startTransaction(linkedDataObject) function. From then on, all transactions will be tracked, but not added to the original ldoDataset. You can view the changes using the transactionChanges(linkedDataObject) or toSparqlUpdate(linkedDataObject) methods. When you're done with the transaction, you can run the commitTransaction(linkedDataObject) method to add the changes to the original ldoDataset.

import {
} from "@ldo/ldo"; 

// ... Get the profile linked data object

startTransaction(profile); = "Kuzon"
const changes = transactionChanges(profile);
// Logs: <> <> "Kuzon"
// Logs: <> <> "Aang"
console.log(await toSparqlUpdate(profile));

Other LDO Helper Functions


Returns the Linked Data Object's underlying RDFJS dataset. Modifying this dataset will change the Linked Data Object as well.

import { getDataset } from "@ldo/ldo"
const dataset = getDataset(profile);