JSON-LD


JSON-LD

In contrast to RDFa and HTML5 Microdata, the two other mainstream formats to add structured data to the web site markup, JavaScript Object Notation for Linked Data (JSON-LD) is described as JavaScript code rather than markup elements and attributes. As a result, JSON-LD is completely separate from the (X)HTML code. One of the advantages of this lightweight Linked Data format is that it is easy for humans to read and write. JSON-LD transports Linked Data using the JavaScript Object Notation (JSON), an open standard format using human-readable text to transmit attribute-value pairs. If the JSON-LD code is written in a separate file rather than the markup, the de facto file extension is .jsonld. The Internet media type of JSON-LD is application/ld+json and, if written in the markup, the JSON-LD code is delimited by curly braces between the <script> and </script> tags, as shown in Listing 1.

Listing 1. Compact JSON-LD Code in the Markup


<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "image": "johnsmith.jpg",
  "name": "John Smith",
  "url": "http://www.johnsmith.com"
}
</script>

This example uses the compact syntax of JSON-LD, which can be expanded to the full syntax notation demonstrated in Listing 2.

Listing 2. Expanded JSON-LD Code


[
  {
    "@type": [
      "http://schema.org/Person"
    ],
    "http://schema.org/image": [
      {
        "@id": "http://www.johnsmith.com/images/johnsmith.jpg"
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "John Smith"
      }
    ],
    "http://schema.org/url": [
      {
        "@id": "http://www.johnsmith.com"
      }
    ]
  }
]

JSON-LD DOM API

The API of JSON-LD provides a way to transform JSON-LD documents to be more easily consumed by Semantic Web applications.

You can read more about JSON-LD in the book Mastering Structured Data on the Semantic Web.