Skip to content

yaml_ld.flatten.flatten

flatten(
    document: JsonLdInput,
    ctx: JsonLdContext | None = None,
    options: FlattenOptions = DEFAULT_FLATTEN_OPTIONS,
) -> JsonLdRecord

Flatten a *-LD document.

Outputs a single array containing all nodes, making no assumptions about their relationships, to simplify data processing and ensure all referenced nodes are included.

Example

flatten.py
import yaml

import yaml_ld

print(yaml.dump(yaml_ld.flatten('pythagorean-theorem.yamlld')))
pythagorean-theorem.yamlld
"@context":
  - https://json-ld.org/contexts/dollar-convenience.jsonld
  - prov: http://www.w3.org/ns/prov#
  - variable: https://variables.namespace/
    =: https://math.namespace/equal
    +: https://math.namespace/add
    ²: https://math.namespace/squared

$id: https://dbpedia.org/page/Pythagorean_theorem
prov:Entity:
  =:
    - +:
      - ²: variable:a
      - ²: variable:b
    - ²: variable:c
python flatten.py
- '@id': _:b0
  https://math.namespace/equal:
  - '@id': _:b1
  - '@id': _:b4
- '@id': _:b1
  https://math.namespace/add:
  - '@id': _:b2
  - '@id': _:b3
- '@id': _:b2
  https://math.namespace/squared:
  - '@value': variable:a
- '@id': _:b3
  https://math.namespace/squared:
  - '@value': variable:b
- '@id': _:b4
  https://math.namespace/squared:
  - '@value': variable:c
- '@id': https://dbpedia.org/page/Pythagorean_theorem
  http://www.w3.org/ns/prov#Entity:
  - '@id': _:b0