Skip to content

jsonld.compact()

Parameters:

Name Type Description Default
input_

the JSON-LD input to compact.

required
ctx Context

the JSON-LD context to compact with.

required
options CompactOptions | None

optional processing options; see Options below.

None

Returns:

Type Description

the compacted JSON-LD output.

Options

base instance-attribute

base: str

The base IRI to use.

compactArrays instance-attribute

compactArrays: bool

True to compact arrays to single values when appropriate, False not to (default: True).

documentLoader instance-attribute

documentLoader: DocumentLoader | DocumentLoaderCallable

The document loader (default: the global default document loader).

expandContext instance-attribute

expandContext: Context

A context to expand with.

extractAllScripts instance-attribute

extractAllScripts: bool

True to extract all JSON-LD script elements from HTML, False to extract just the first (default: False).

graph instance-attribute

graph: bool

True to always output a top-level graph (default: False).

processingMode instance-attribute

processingMode: Literal['json-ld-1.0', 'json-ld-1.1']

Either json-ld-1.0 or json-ld-1.1 (default: json-ld-1.1).

Example

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