Linked Data is a way of creating graph data structures.
Graph data structures are about relations.
What does JSON-LD Do?
JSON-LD separates the data from the format. Weird.
JSON-LD contextualizes the JSON object and its’ data.
JSON-LD removes ambiguity of keys.
Removing Ambiguity
Consider the following JSON object:
{
id: 0x01,
name: "Widget"
}
What does that mean? How do we use this object? Removed from the context of the application and/or service documentation we don’t know what this object is or what these keys are for. We can make some assumptions, but we can’t be sure.
We can turn this into JSON-LD by adding a @context.
@context is a key/value object that associates ambiguous key names with unambiguous URIs. Like so:
Note the introduction of the @graph key – this is how we gather multiple objects together into a single, contextualized object. Every object in the @graph array is a Subject in the data.
Separate the data from the format
JSON keys are fundamentally arbitrary. As is the structure of the JSON. This is why we can use GraphQL to request JSON blobs of different shapes and nested objects.
JSON-LD gives us a way to take the underlying data being represented by an object and decouple it from the JSON format itself.
It does this by converting a single object with key/value pairs into a number of tuples, in this case 3-tuples (also knows as “triples” for normal non math humans).
The two objects above, in Context One, look like this when represented as triples:
When we make transformations on JSON-LD objects we are storing the underlying data in-memory as triples, which are very fast and easy for code to work with. We then request transformations and particular shapes of this data to use in our application.
@base
We’ve introduced another keyword here — @base. One of the core principles of JSON-LD is that every node or object needs it’s own URI as a way to identify it. A string like thing-two-slugisn’t a URI, but if we append it to a URI protocol is can be: uri:thing-two-slug. More commonly, this is a URL like https://hefty.mart, which would give is the URIhttps://hefty.mart/thing-two-slug.
Data with Relationships
Let’s say our two things are manufactured by the same company, HeftyMart™.
{
url: https://hefty.mart
name: "HeftyMart™"
location: "The County"
}
Lets contextualize this object:
{
@context: {
url: @id,
name: uri:uuid:company-name,
location: uri:uuid:physical-place
},
url: https://hefty.mart
name: "HeftyMart™"
location: "The County"
}
Note how “name” in this context means something different then in our previous contexts. We are making a statement that “company names” and different sorts of things than “thing names”.
Now we have three objects, each contextualized. As before, we can harmonize these objects into a single JSON-LD object, and convert the resulting object into the underlying data as triples:
Graphs are mathematical structures composed of “nodes” (which we can think of as Objects in this case) and “edges” (which we can think of as relationships).
We have three nodes in this graph. Can we add relationships?
Another keyword! This one is for defining relationships between keys. By declaring in our context that makesThings is the reverse of madeBy, we are setting up a two-way relationship that allows is to list all the nodes with madeBy: "https://hefty.mart" under HeftyMarts makesThings key. Since the underlying data is strucutred as a triple, we’re really just writing a rule that informs another way we can convert that triple into a key-value pair on an object.
More about frames
The frames spec is suprisingly readable, and goes into full detail about what’s possible with JSON-LD frames.
JSON-LD in the wild
To put JSON-LD on a webpage, all you have to do is stick your blob into a script tag with a type declaration:
You can have as many of these blobs on the page as you want! For example, this How-To article on the Verge has two. One contains data about the article, and the other contains specific data on the How-To structure of the article.
Tools like Schema.orgs validator can fetch the markup of the page, extract the JSON-LD blobs, and then operate on the data.
This is how Google search uses JSON-LD for improving search results and SEO – by adding that data to our pages we are making visible information in the from of triples, ie:
This seperates the content of our data from our own structure, and allows Google to mesh it into their own knowledge graph systems.
To Sum Up
JSON-LD is a powerful tool for manipulating data – it lets us harmonize data from different sources, transform the shape of that data, and allows for an easy interface for making our data interoperable with other systems.