W3C home > Mailing lists > Public > public-linked-json@w3.org > March 2013

JSON-LD in HTML

From: Gregg Kellogg <gregg@kellogg-assoc.com>
Date: Sat, 16 Mar 2013 14:01:40 -0700
Message-Id: <7232B5C1-EE7F-4A76-AFE1-AE44CAD96B84@me.com>
To: "public-rdf-wg@w3.org WG" <public-rdf-wg@w3.org>, JSON-LD <public-linked-json@w3.org>
This proposal provides a means for embedding JSON-LD in HTML documents, much in the same way that Turtle can be embedded in HTML documents. This is important for certain constituencies who have shown problems in providing correct RDFa or microdata, but who are familiar with JSON. Embedding JSON-LD in HTML, along with a JSON-LD context, allows fairly straight-forward JSONto be used for specifying metadata within an HTML document.

The use of the @data-context attribute below is not ideal, as @data-* attributes are intended for use by other specifications, but it seems inappropriate to either re-purpose some existing HTML (or HTML+RDFa) attribute, or to define a new attribute specifically for this narrow case.

After section 6.8 in JSON-LD Syntax, add the following section:

Section 6.9 (new/non-normative) Embedding JSON-LD in HTML documents

HTML script tags can be used to embed blocks of data in documents. JSON-LD can be easily embedded in HTML this way.

<script type="application/ld+json">
{
 "@context": {
   "@vocab": "http://purl.org/vocab/frbr/core#",
   "@language": "en",
   "dc": "http://purl.org/dc/terms/",
 },
 "@id": "http://books.example.com/works/45U8QJGZSQKDH8N",
 "@type": "Work",
 "dc:creator": "Wil Wheaton",
 "dc:title": "Just a Geek",
 "realization": [{
   "@id": "http://books.example.com/products/9780596007683.BOOK",
   "@type": "Expression";
   "dc:type": "http://books.example.com/product-types/BOOK"
 }, {
   "@id": "http://books.example.com/products/9780596802189.EBOOK"
   "dc:type": "http://books.example.com/product-types/EBOOK"
 }]
}
</script>

JSON-LD content should be placed in a script element with the @type attribute set to application/ld+json. For text/html, text inside of the script tags does not need to be escaped (issue: use CDATA for XHTML?). The character encoding of the embedded JSON-LD fragment will match the HTML documents encoding.

If a processor extracts the JSON-LD content into RDF, it should expand the JSON-LD fragment into an RDF dataset using the algorithm defined in [JSON-LD-Processing] section 10.6 "Convert to RDF Algorithm". If the HTML file contains multiple JSON-LD script tags, or other RDF statements are extracted, the result is the RDF merge of the datasets.

Other processors implementing this mechanism may choose to expand the JSON-LD and return the expanded JSON-LD output.

Section 6.9.1 Specifying Remote Context

In addition to the mechanism defined in section 6.9, if the @data-context attribute is present on the script element, its value is provided to the JSON-LD processor in a manner equivalent to the treatment of the HTTP Link Header with the http://www.w3.org/ns/json-ld#context link relation as specified in section 6.8, except that the inline specified JSON-LD need not have have embedded context information. For example:

<script type="application/ld+json" data-context="http://schema.org/">
{
 "@type": "Book",
 "image": "catcher-in-the-rye-book-cover.jpg",
 "name": "The Catcher in the Rye",
 "bookFormat": "Paperback",
 "author": "/author/jd_salinger.html",
 "aggregateRating": {
   "@type": "AggregateRating",
   "ratingValue": 4,
   "reviewCount": 3077
 },
 "offers": {
   "@type": "Offer",
   "price": 6.99,
   "priceCurrency": "USD",
   "availability": "InStock"
 },
 "numberOfPages": 244,
 "publisher": "Little, Brown, and Company",
 "datePublished": "1991-05-01",
 "inLanguage": "English",
 "isbn": "0316769487"
}
</script>

This allows the context to be separated from the document, which particularly simplifies the case when there are multiple top-level nodes defined within the document, which would otherwise require using @graph.

In the proceeding example, a JSON-LD processor will use content-negotiation to return a JSON-LD context document from `http://schema.org/` (presuming that it would return a JSON-LD context if the Accept header asks for application/ld+json), which will then be applied to the to the document contained within the script element.

Gregg Kellogg
greggkellogg@me.com
Received on Saturday, 16 March 2013 21:02:10 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:53:21 UTC