pyld (JSON-LD API for Python)

3.0.0 · active · verified Thu Apr 16

PyLD is a Python implementation of a JSON-LD processor, providing tools for working with Linked Data in JSON. It offers core JSON-LD operations such as compaction, expansion, flattening, and framing, adhering to the JSON-LD 1.1 specification. The library is actively maintained, with its current version 3.0.0 released in February 2024. While there isn't a fixed public release cadence, the project shows ongoing development and significant updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionalities of `pyld` including `compact`, `expand`, and `flatten` operations using a sample JSON-LD document and context. It processes the document to show its compacted, expanded, and flattened forms.

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"}
}

# Compact a document according to a particular context
compacted = jsonld.compact(doc, context)
print("Compacted document:")
print(json.dumps(compacted, indent=2))

# Expand a document, removing its context
expanded = jsonld.expand(compacted)
print("\nExpanded document:")
print(json.dumps(expanded, indent=2))

# Flatten a document
flattened = jsonld.flatten(doc)
print("\nFlattened document:")
print(json.dumps(flattened, indent=2))

view raw JSON →