PyLD¶
PyLD is a Python implementation of the JSON-LD processor API.
JSON-LD is a lightweight syntax for expressing Linked Data in JSON. It lets applications add meaning to existing JSON documents with in-band or out-of-band contexts, while keeping the document shape practical for web APIs, JavaScript, and JSON document stores.
Quick Examples¶
Compacts a JSON-LD document with a context, replacing full IRIs with shorter terms where possible. Read more
Example compact.py
import json
from pyld import jsonld
doc = {
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}
context = {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
}
compacted = jsonld.compact(doc, context)
print(json.dumps(compacted, indent=2))
Expands a compacted JSON-LD document into full IRI-based form and removes the context. Read more
Example expand.py
import json
from pyld import jsonld
doc = {
"@context": {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
},
"image": "http://manu.sporny.org/images/manu.png",
"homepage": "http://manu.sporny.org/",
"name": "Manu Sporny",
}
expanded = jsonld.expand(doc)
print(json.dumps(expanded, indent=2))
Flattens nested JSON-LD into a top-level node map so each node can be processed independently. Read more
Example flatten.py
Frames expanded JSON-LD into a predictable tree shape that matches a supplied frame. Read more
Example frame.py
import json
from pyld import jsonld
doc = {
"@id": "http://example.com/people/manu",
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}
frame = {
"@context": {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
},
"@type": "http://schema.org/Person",
}
framed = jsonld.frame(doc, frame)
print(json.dumps(framed, indent=2))
{
"@context": {
"name": "http://schema.org/name",
"homepage": {
"@id": "http://schema.org/url",
"@type": "@id"
},
"image": {
"@id": "http://schema.org/image",
"@type": "@id"
}
},
"@id": "http://example.com/people/manu",
"@type": "http://schema.org/Person",
"image": "http://manu.sporny.org/images/manu.png",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
Converts a JSON-LD document into RDF statements in a requested serialization format. Read more
Example to_rdf.py
Converts RDF statements into JSON-LD so the data can be processed with the JSON-LD API. Read more
Example from_rdf.py
Normalizes JSON-LD into canonical RDF statements for stable comparison, hashing, or signing. Read more
Example normalize.py
from pyld import jsonld
doc = {
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}
normalized = jsonld.normalize(
doc,
{"algorithm": "URDNA2015", "format": "application/n-quads"},
)
print(normalized)
Maintainers¶
-
Miel Vander Sande
-
Anatoly Scherbakov
-
Digital Bazaar
@digitalbazaar
Digital Bazaar, Inc.
digitalbazaar.com
Original authors of
PyLD


