Python implementation of the JSON-LD API for processing Linked Data in JSON format
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Bidirectional conversion between JSON-LD and RDF dataset formats, enabling interoperability with RDF triple stores, semantic web applications, and other RDF-based tools.
Converts JSON-LD documents to RDF dataset format, extracting semantic triples and enabling integration with RDF processing systems.
def to_rdf(input_, options=None):
"""
Converts JSON-LD document to RDF dataset.
Args:
input_: The JSON-LD document to convert (dict, list, or str)
options: Optional conversion settings (dict)
Options:
base (str): The base IRI to use for resolving relative IRIs
format (str): Output format ('application/n-quads' for N-Quads string)
produceGeneralizedRdf (bool): True to output generalized RDF with
non-standard features (default: False)
extractAllScripts (bool): True to extract all JSON-LD script elements
from HTML (default: True)
processingMode (str): JSON-LD processing mode ('json-ld-1.0' or
'json-ld-1.1', default: 'json-ld-1.1')
documentLoader (function): Custom document loader function
Returns:
dict or str: RDF dataset dict, or N-Quads string if format specified
Raises:
JsonLdError: If conversion fails due to invalid JSON-LD input
"""from pyld import jsonld
doc = {
"@context": {
"name": "http://schema.org/name",
"Person": "http://schema.org/Person"
},
"@id": "http://example.org/person/1",
"@type": "Person",
"name": "Alice"
}
# Convert to RDF dataset
rdf_dataset = jsonld.to_rdf(doc)
# Convert to N-Quads format
nquads = jsonld.to_rdf(doc, {'format': 'application/n-quads'})
print(nquads)
# Output: <http://example.org/person/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
# <http://example.org/person/1> <http://schema.org/name> "Alice" .Converts RDF datasets or serialized RDF strings to JSON-LD format, enabling JSON-based processing of RDF data.
def from_rdf(input_, options=None):
"""
Converts RDF dataset to JSON-LD document.
Args:
input_: RDF dataset dict or serialized RDF string to convert
options: Optional conversion settings (dict)
Options:
format (str): Input format when input is string ('application/n-quads'
for N-Quads, default: 'application/n-quads')
useRdfType (bool): True to use rdf:type, False to use @type
(default: False)
useNativeTypes (bool): True to convert XSD types to native Python types
(bool, int, float), False to keep as strings
(default: True)
Returns:
list: JSON-LD document as expanded array
Raises:
JsonLdError: If conversion fails due to invalid RDF input
"""from pyld import jsonld
# N-Quads string input
nquads = '''
<http://example.org/person/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
<http://example.org/person/1> <http://schema.org/name> "Alice" .
'''
# Convert from N-Quads to JSON-LD
json_ld = jsonld.from_rdf(nquads)
print(json_ld)
# Output: [{"@id": "http://example.org/person/1", "@type": ["http://schema.org/Person"], "http://schema.org/name": [{"@value": "Alice"}]}]
# Convert with native types
json_ld_native = jsonld.from_rdf(nquads, {'useNativeTypes': True})
# Use rdf:type instead of @type
json_ld_rdf_type = jsonld.from_rdf(nquads, {'useRdfType': True})When working with RDF datasets (not serialized strings), PyLD uses this internal structure:
# RDF Dataset format
{
"@default": [ # Default graph triples
{
"subject": {"@id": "http://example.org/subject"},
"predicate": {"@id": "http://example.org/predicate"},
"object": {"@value": "literal value", "@type": "http://www.w3.org/2001/XMLSchema#string"}
}
],
"http://example.org/graph": [ # Named graph triples
# ... triples in named graph
]
}
# RDF Triple format
{
"subject": {"@id": "http://example.org/subject"}, # IRI or blank node
"predicate": {"@id": "http://example.org/predicate"}, # IRI only
"object": { # IRI, blank node, or literal
"@id": "http://example.org/object" # For IRI/blank node
# OR
"@value": "literal value", # For literals
"@type": "http://www.w3.org/2001/XMLSchema#string", # Optional datatype
"@language": "en" # Optional language tag
}
}PyLD supports pluggable RDF parsers for different serialization formats:
def register_rdf_parser(content_type, parser):
"""
Register a custom RDF parser for a content type.
Args:
content_type (str): MIME type to associate with parser
parser (function): Parser function that takes RDF string and returns dataset
"""
def unregister_rdf_parser(content_type):
"""
Unregister an RDF parser for a content type.
Args:
content_type (str): MIME type to unregister
"""from pyld import jsonld
def custom_turtle_parser(rdf_string):
# Custom parser implementation
# Must return RDF dataset in PyLD format
pass
# Register parser
jsonld.register_rdf_parser('text/turtle', custom_turtle_parser)
# Use with from_rdf
turtle_data = "@prefix ex: <http://example.org/> . ex:subject ex:predicate ex:object ."
json_ld = jsonld.from_rdf(turtle_data, {'format': 'text/turtle'})When useNativeTypes is True, XSD datatypes are converted to Python types:
| XSD Type | Python Type | Example |
|---|---|---|
xsd:boolean | bool | true → True |
xsd:integer | int | "42" → 42 |
xsd:double | float | "3.14" → 3.14 |
xsd:string | str | "hello" → "hello" |
Blank nodes are represented with special @id values:
# Blank node in RDF dataset
{
"subject": {"@id": "_:b0"}, # Blank node identifier
"predicate": {"@id": "http://example.org/predicate"},
"object": {"@value": "value"}
}RDF conversion functions may raise JsonLdError with these specific error types:
# Export JSON-LD to triple store
json_ld_doc = {...}
nquads = jsonld.to_rdf(json_ld_doc, {'format': 'application/n-quads'})
# Store nquads in triple store
# Import from triple store
nquads_from_store = get_nquads_from_triple_store()
json_ld = jsonld.from_rdf(nquads_from_store)# Convert between formats in data pipeline
def process_semantic_data(input_data, input_format, output_format):
if input_format == 'json-ld' and output_format == 'nquads':
return jsonld.to_rdf(input_data, {'format': 'application/n-quads'})
elif input_format == 'nquads' and output_format == 'json-ld':
return jsonld.from_rdf(input_data, {'format': 'application/n-quads'})Install with Tessl CLI
npx tessl i tessl/pypi-pyld