Skip to content

PyLD

Main CI PyPI Downloads

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))
Output
{
  "@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",
  "name": "Manu Sporny",
  "homepage": "http://manu.sporny.org/"
}

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))
Output
[
  {
    "http://schema.org/url": [
      {
        "@id": "http://manu.sporny.org/"
      }
    ],
    "http://schema.org/image": [
      {
        "@id": "http://manu.sporny.org/images/manu.png"
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "Manu Sporny"
      }
    ]
  }
]

Flattens nested JSON-LD into a top-level node map so each node can be processed independently. Read more

Example flatten.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"
    },
}

flattened = jsonld.flatten(doc)

print(json.dumps(flattened, indent=2))
Output
[
  {
    "@id": "_:b0",
    "http://schema.org/image": [
      {
        "@id": "http://manu.sporny.org/images/manu.png"
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "Manu Sporny"
      }
    ],
    "http://schema.org/url": [
      {
        "@id": "http://manu.sporny.org/"
      }
    ]
  }
]

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))
Output
{
  "@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

from pyld import jsonld

doc = {
    "@context": {
        "name": "http://schema.org/name",
    },
    "@id": "http://dbpedia.org/resource/Earth",
    "name": "Earth",
}

print(jsonld.to_rdf(doc, {"format": "application/n-quads"}))
Output
<http://dbpedia.org/resource/Earth> <http://schema.org/name> "Earth" .

Converts RDF statements into JSON-LD so the data can be processed with the JSON-LD API. Read more

Example from_rdf.py

import json

from pyld import jsonld

nquads = (
    '<http://dbpedia.org/resource/Earth> '
    '<http://schema.org/name> "Earth" .\n'
)

doc = jsonld.from_rdf(nquads, {"format": "application/n-quads"})

print(json.dumps(doc, indent=2))
Output
[
  {
    "@id": "http://dbpedia.org/resource/Earth",
    "http://schema.org/name": [
      {
        "@value": "Earth"
      }
    ]
  }
]

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)
Output
_:c14n0 <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .
_:c14n0 <http://schema.org/name> "Manu Sporny" .
_:c14n0 <http://schema.org/url> <http://manu.sporny.org/> .
_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .

Maintainers