Skip to main content
Dublin Library

The Publishing Project

JSON-LD on the web


JSON-LD is a set of extensions to JSON that allows for creating and exchanging linked data on the web.

Perhaps the best-known use of JSON-LD outside semantic web users is using's JSON-LD structured data for Google's search engine.

The following example gives the structured data snippet to accompany a recipe web page. The example is taken from Understand how structured data works.

    <title>Party Coffee Cake</title>
    <script type="application/ld+json">
      "@context": "",
      "@type": "Recipe",
      "name": "Party Coffee Cake",
      "author": {
        "@type": "Person",
        "name": "Mary Stone"
      "datePublished": "2018-03-10",
      "description": "This coffee cake is awesome and perfect for parties.",
      "prepTime": "PT20M"
    <h2>Party coffee cake recipe</h2>
      <em>by Mary Stone, 2018-03-10</em>
      This coffee cake is awesome and perfect for parties.
      Preparation time: 20 minutes

We can also leverage the JSON-LD snippet that we created for Google Search and create our own snippets and content highlights. The only change we make to the script is an ID attribute to the script to make it easier to capture.

The process is as follows:

  1. Grab the text of the script with ID data
  2. Parse the JSON
  3. Create a template literal interpolating the data from the JSON file
  4. Grab a reference to the container where we want to place the template data
  5. Insert the template literal into the container using insertAdjacentHTML
const data = document.getElementById("data").text; // 1
const json = JSON.parse(data); // 2

const displayData = `
<h3>by ${}</h3>
`; // 3

const container = document.getElementById("container"); //4
container.insertAdjacentHTML("afterend", displayData); // 5

Another aspect of JSON-LD is how to create the linked data scripts. Even a trivial example like that of our recipe requires some knowledge of the standard and it is not a trivial undertaking. Since I work with WordPress for the most part, I've chosen to either use a WordPress schema plugin to let WordPress build the Linked Data automatically or tools like scheema-org to generate the Linked Data scripts manually for whatever type of page I'm working on, inside or outside of WordPress.

The following example covers most of the information for the 50th-anniversary edition of Rayuela (Hopscotch) in Spanish.

use Spatie\SchemaOrg\Schema;

$rayuela = Schema::Book()
    ->name('Julio Cortázar')
  ->description('Rayuela es la historia de Horacio Oliveira, un intelectual argentino, y su relación con Lucía, una joven uruguaya apodada "La Maga".')

echo $rayuela->toScript();

It will generate the following JSON-LD inside a script tag, ready to insert on an HTML document and that runs without errors or warnings in the validator

<script type="application/ld+json">
    "name":"Julio Cortázar"
  "description":"Rayuela es la historia de Horacio Oliveira, un intelectual argentino, y su relación con Lucía, una joven uruguaya apodada \"La Maga\".",
  "publisher": {

Even if you decide that you don't want rich snippets on your site's results in Google Search, JSON-LD and Linked Data open other possibilities.

Edit on Github