Skip to content

jsonld.flatten()

Parameters:

Name Type Description Default
input_

the JSON-LD input to flatten.

required
ctx Context | None

the JSON-LD context to compact with (default: None).

None
options FlattenOptions | None

optional processing options; see Options below.

None

Returns:

Type Description

the flattened JSON-LD output.

Options

base instance-attribute

base: str

The base IRI to use.

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: True).

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