Skip to content

jsonld.to_rdf()

Parameters:

Name Type Description Default
input_

the JSON-LD input.

required
options ToRdfOptions | None

optional processing options; see Options below.

None

Returns:

Type Description

the resulting RDF dataset (or a serialization of it).

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

extractAllScripts instance-attribute

extractAllScripts: bool

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

format instance-attribute

format: Literal['application/n-quads']

The format to use to output a string: application/n-quads for N-Quads.

identifierIssuer instance-attribute

identifierIssuer: IdentifierIssuer

An identifier issuer to use for blank node identifiers (default: IdentifierIssuer('_:b')).

legacyMode instance-attribute

legacyMode: bool

True to return the RDF.js-like dataset dict used by PyLD versions lower than 4 instead of an rdflib.Dataset when format is not set (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).

produceGeneralizedRdf instance-attribute

produceGeneralizedRdf: bool

True to output generalized RDF, False to produce only standard RDF (default: False).

rdfDirection instance-attribute

rdfDirection: Literal['i18n-datatype', 'compound-literal']

Either i18n-datatype or compound-literal is supported (default: None).

When format is not set, jsonld.to_rdf() returns a RDFLib rdflib.Dataset. Set legacyMode to True to return the RDF.js-like dataset dict used by PyLD versions lower than 4.

legacyMode only affects the unformatted return value. If format is set, jsonld.to_rdf() returns the requested serialization.

Examples

Serializing JSON-LD to N-Quads

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"^^<http://www.w3.org/2001/XMLSchema#string>  .

Transforming JSON-LD into an rdflib.Dataset

Example to_rdf_dataset.py

from pyld import jsonld

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

dataset = jsonld.to_rdf(doc)

print(type(dataset).__name__)
print(dataset.serialize(format="nquads"))
Output
Dataset
<http://dbpedia.org/resource/Earth> <http://schema.org/name> "Earth"^^<http://www.w3.org/2001/XMLSchema#string>  .

Transforming JSON-LD into an legacy RDF.js-like dict

Example to_rdf_legacy.py

import json

from pyld import jsonld

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

legacy_dataset = jsonld.to_rdf(doc, {"legacyMode": True})

print(json.dumps(legacy_dataset, indent=2))
Output
{
  "@default": [
    {
      "subject": {
        "type": "IRI",
        "value": "http://dbpedia.org/resource/Earth"
      },
      "predicate": {
        "type": "IRI",
        "value": "http://schema.org/name"
      },
      "object": {
        "type": "literal",
        "value": "Earth",
        "datatype": "http://www.w3.org/2001/XMLSchema#string"
      }
    }
  ]
}