html_json_ld: JSON-LD blocks

View source: R/metadata.R

html_json_ldR Documentation

JSON-LD blocks

Description

The structured data pages embed as JSON-LD: the text of each ⁠<script type="application/ld+json">⁠ below the given nodes (and the nodes themselves), in document order. The type is matched as a MIME type: case-insensitively, ignoring surrounding whitespace and any parameters.

Usage

html_json_ld(x, parse = FALSE)

Arguments

x

A zuhtml_document or zuhtml_nodeset.

parse

If TRUE, parse each block with jsonlite::fromJSON() (with simplifyVector = FALSE, so objects are named lists and arrays are lists). This needs the jsonlite package. A wrapper around the whole block, ⁠<![CDATA[ ... ]]>⁠ or ⁠<!-- ... -->⁠, optionally commented out with ⁠//⁠ or ⁠/* */⁠ as pages often do, is removed first.

Value

With parse = FALSE, a character vector: the text of each block, exactly as written. With parse = TRUE, a list as long as that vector: each block's parsed value, or NULL for a block that is not valid JSON; the texts are kept in its attribute "json".

See Also

Other metadata: html_meta(), html_microdata(), html_title()

Examples

doc <- html_parse(paste0(
  "<script type='application/ld+json'>",
  '{"@context": "https://schema.org", "@type": "Person", "name": "Ada"}',
  "</script>"
))
html_json_ld(doc)
if (requireNamespace("jsonlite", quietly = TRUE)) {
  html_json_ld(doc, parse = TRUE)[[1]]$name
}

zuhtml documentation built on Oct. 6, 2026, 5:06 p.m.