XML technologies / JSON for XML professionals / JSON-LD and semantic data
The word “semantic” is used in different ways in publishing projects. An element author or a field author can already provide meaningful domain-specific markup. Linking statements across systems, however, requires additional agreements: which person is meant? Which vocabulary describes the relationship? How are identical resources recognized? JSON alone does not answer these questions, and neither does XML alone.
From labeled fields to identified resources
The following conventional JSON object deliberately shows a shortened extract of the book description with one of the three authors:
{
"title": "Schematron",
"author": "Manuel Montero Pineda"
}An application can know its meaning because it was programmed according to a particular contract. A separate data source, however, does not automatically know whether an author with the same name is the same person. Even a field authorId solves the problem only within the agreed identification space.
JSON-LD extends JSON with a standardized model that, among other things, links terms to IRIs and identifies resources. @context, @id and @type are keywords with defined meanings. JSON-LD can be processed as JSON while also being designed for use with RDF technologies. [Q07]
The short object above is deliberately limited to one author. The following semantic example, by contrast, includes all three book authors, the ISBN and the publisher. It also connects Manuel Montero Pineda with data2type GmbH; no company affiliation is asserted for the two coauthors. This is still not a complete conversion of all fields in the previous book file: the invented sales data is not presented as actual book offers. [Q43] The example uses an embedded context object, so no context has to be downloaded from the internet for the test:
File: data/book.jsonld
{
"@context": {
"schema": "https://schema.org/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"name": "schema:name",
"author": {
"@id": "schema:author",
"@type": "@id"
},
"datePublished": {
"@id": "schema:datePublished",
"@type": "xsd:date"
},
"inLanguage": "schema:inLanguage",
"isbn": "schema:isbn",
"publisher": {
"@id": "schema:publisher",
"@type": "@id"
},
"affiliation": {
"@id": "schema:affiliation",
"@type": "@id"
},
"url": {
"@id": "schema:url",
"@type": "@id"
}
},
"@id": "https://example.org/data2type/books/book-001",
"@type": "schema:Book",
"name": {
"@value": "Schematron: Effiziente Business Rules für XML-Dokumente",
"@language": "de"
},
"datePublished": "2011-07-27",
"inLanguage": "de",
"isbn": "9783898647212",
"url": "https://www.data2type.de/publikationen/schematron/",
"publisher": {
"@id": "https://example.org/data2type/organizations/dpunkt",
"@type": "schema:Organization",
"name": "dpunkt.verlag"
},
"author": [
{
"@id": "https://example.org/data2type/people/person-001",
"@type": "schema:Person",
"name": "Manuel Montero Pineda",
"affiliation": {
"@id": "https://example.org/data2type/organizations/data2type",
"@type": "schema:Organization",
"name": "data2type GmbH",
"url": "https://www.data2type.de/"
}
},
{
"@id": "https://example.org/data2type/people/person-002",
"@type": "schema:Person",
"name": "Marko Hedler"
},
{
"@id": "https://example.org/data2type/people/person-003",
"@type": "schema:Person",
"name": "Nico Kutscherauer"
}
]
}The term name maps to schema.org/name. The book’s IRI is independent of its visible label. Each author has a separate example IRI and can be reused in other statements. publisher refers to dpunkt.verlag, whereas affiliation refers to data2type for Manuel Montero Pineda. These roles are not mixed. The real website URLs are in the field url; the local @id values do not claim to provide a production Linked Data interface. The Schema.org vocabulary defines, among other things, the type Book and properties for describing publications. [Q23]
A language-tagged value is used for the book’s name. The context assigns a data type to the publication date. These are meaningful domain distinctions: a language declaration for the entire record is not the same as a language tag on an individual literal.
The same RDF graph in RDF/XML
The same small graph model can also be expressed in RDF/XML:
File: data/book.rdf
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:schema="https://schema.org/">
<schema:Book rdf:about="https://example.org/data2type/books/book-001">
<schema:name xml:lang="de">Schematron: Effiziente Business Rules für XML-Dokumente</schema:name>
<schema:datePublished rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-07-27</schema:datePublished>
<schema:inLanguage>de</schema:inLanguage>
<schema:isbn>9783898647212</schema:isbn>
<schema:url rdf:resource="https://www.data2type.de/publikationen/schematron/"/>
<schema:publisher rdf:resource="https://example.org/data2type/organizations/dpunkt"/>
<schema:author rdf:resource="https://example.org/data2type/people/person-001"/>
<schema:author rdf:resource="https://example.org/data2type/people/person-002"/>
<schema:author rdf:resource="https://example.org/data2type/people/person-003"/>
</schema:Book>
<schema:Person rdf:about="https://example.org/data2type/people/person-001">
<schema:name>Manuel Montero Pineda</schema:name>
<schema:affiliation rdf:resource="https://example.org/data2type/organizations/data2type"/>
</schema:Person>
<schema:Person rdf:about="https://example.org/data2type/people/person-002">
<schema:name>Marko Hedler</schema:name>
</schema:Person>
<schema:Person rdf:about="https://example.org/data2type/people/person-003">
<schema:name>Nico Kutscherauer</schema:name>
</schema:Person>
<schema:Organization rdf:about="https://example.org/data2type/organizations/dpunkt">
<schema:name>dpunkt.verlag</schema:name>
</schema:Organization>
<schema:Organization rdf:about="https://example.org/data2type/organizations/data2type">
<schema:name>data2type GmbH</schema:name>
<schema:url rdf:resource="https://www.data2type.de/"/>
</schema:Organization>
</rdf:RDF>RDF/XML is an XML syntax for RDF, not a name for arbitrary XML with meaningful element names. Conversely, JSON-LD is not simply a JSON object in which the field @context happens to occur. Both forms must be interpreted according to their respective rules. [Q22]
Comparing the files as text would reveal little here. The relevant comparison concerns the resulting RDF graph. The companion program reads both forms and checks graph isomorphism. This is also the correct principle when a graph contains blank nodes whose actual internal identifiers may differ.
File: examples/python/semantic_graph.py
"""Compare RDF meaning, not JSON/XML spelling. No remote context is used."""
import json
from pathlib import Path
from rdflib import Graph
from rdflib.compare import isomorphic
ROOT = Path(__file__).resolve().parents[2]
jsonld = Graph().parse(ROOT / "data/book.jsonld", format="json-ld")
rdfxml = Graph().parse(ROOT / "data/book.rdf", format="xml")
rows = jsonld.query((ROOT / "examples/sparql/books.rq").read_text())
print(json.dumps({
"sameGraph": isomorphic(jsonld, rdfxml),
"tripleCount": len(jsonld),
"books": [{"title": str(row.title), "authorName": str(row.authorName)}
for row in rows]
}, ensure_ascii=False, indent=2))Actual result of the included check:
File: results/core/semantic-graph.json
{
"sameGraph": true,
"tripleCount": 22,
"books": [
{
"title": "Schematron: Effiziente Business Rules für XML-Dokumente",
"authorName": "Manuel Montero Pineda"
},
{
"title": "Schematron: Effiziente Business Rules für XML-Dokumente",
"authorName": "Marko Hedler"
},
{
"title": "Schematron: Effiziente Business Rules für XML-Dokumente",
"authorName": "Nico Kutscherauer"
}
]
}The example shows that semantic expressiveness here depends on the jointly defined graph model, not on whether curly or angle brackets are used. JSON-LD is therefore not inherently “more semantic” than RDF/XML. It offers different integration and notation advantages, especially in JSON-oriented web workflows.
Arrays do not automatically represent ordered RDF relationships
In ordinary JSON, an array is ordered. For JSON-LD, one must additionally consider how the array maps to RDF. Ordinary multiple values of a property do not automatically express a domain-specific order in the graph. JSON-LD provides dedicated constructs for explicitly ordered lists. [Q07]
The following example defines a list container for the author list. It uses the custom property pub:authorSequence. This avoids assuming that an external vocabulary automatically permits an RDF list value for an arbitrary property:
File: data/ordered-authors.jsonld
{
"@context": {
"authors": {
"@id": "pub:authorSequence",
"@type": "@id",
"@container": "@list"
},
"pub": "https://example.org/data2type/vocab/"
},
"@id": "https://example.org/data2type/books/book-001",
"authors": [
"https://example.org/data2type/people/person-001",
"https://example.org/data2type/people/person-002",
"https://example.org/data2type/people/person-003"
]
}The test swaps the authors once. Without a list model, the swap produces the same RDF graph. With @list the graph structure changes. For a publishing project, this is not an academic nicety: the order of authors, chapters or contributions may be an explicit business requirement.
These three concepts should therefore be kept separate: JSON array order, an RDF set of statements, and a domain-specific ordered list. An application that treats them as equivalent can lose meaning during an apparently harmless format conversion.
Processing and validation at graph level
A SPARQL query operates on the RDF model and therefore does not depend on the original JSON-LD or RDF/XML notation. The executed example query is:
File: examples/sparql/books.rq
PREFIX schema: <https://schema.org/>
SELECT ?title ?authorName
WHERE {
?book a schema:Book ; schema:name ?title ; schema:author ?author .
?author schema:name ?authorName .
}
ORDER BY ?title ?authorNameSHACL defines shapes for additional structural rules on RDF graphs. The small example file requires a name and at least one author relationship whose target is an IRI. [Q24]
File: schemas/book-shape.ttl
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix schema: <https://schema.org/> .
@prefix ex: <https://example.org/data2type/shapes/> .
ex:BookShape a sh:NodeShape ;
sh:targetClass schema:Book ;
sh:property [ sh:path schema:name ; sh:minCount 1 ; sh:nodeKind sh:Literal ] ;
sh:property [ sh:path schema:author ; sh:minCount 1 ; sh:nodeKind sh:IRI ] .JSON Schema validation and SHACL validation are not interchangeable. The former can constrain the concrete shape of JSON-LD input. The latter examines the statements derived from it. Differently compacted JSON-LD files can describe the same graph while having different JSON structures.
JSON-LD defines its own operations, including expansion, compaction and flattening. These are model-related processing steps, not merely formatting with different indentation. The package prepares an expansion using PyLD and explicitly blocks external context retrieval. Its runtime status is documented separately from the RDF graph check that has already run. [Q34]
Named graphs introduce another edge case: JSON-LD can represent RDF datasets, whereas an ordinary RDF/XML file serializes one RDF graph. A dataset containing several named graphs should therefore not be forced into a single RDF/XML file without further agreement.
JSON-LD in web publishing
Google documents JSON-LD as the recommended format for many uses of structured data on web pages. The data is typically embedded in a script element has type application/ld+json element. This describes the visible page; it does not replace its content. Requirements depend on the particular search feature; valid markup does not guarantee any special appearance in search results. [Q25]
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Book",
"@id": "https://example.org/data2type/books/book-001",
"name": "Schematron",
"inLanguage": "de"
}
</script>This is a simple embedding example, not a promise of a particular search feature. In a publishing pipeline, the JSON-LD description should be derived from the same controlled source as the visible metadata. Otherwise, contradictions can easily arise between the page, API and semantic markup.