CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyld

Python implementation of the JSON-LD API for processing Linked Data in JSON format

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

rdf-conversion.mddocs/

RDF Conversion

Bidirectional conversion between JSON-LD and RDF dataset formats, enabling interoperability with RDF triple stores, semantic web applications, and other RDF-based tools.

Capabilities

JSON-LD to RDF Conversion

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

Example

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

RDF to JSON-LD Conversion

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

Example

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

RDF Dataset Structure

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

Conversion Options

to_rdf() Options

  • format: When set to 'application/n-quads', returns N-Quads string instead of dataset object
  • produceGeneralizedRdf: Allows non-standard RDF features like literals as subjects
  • base: Base IRI for resolving relative references
  • extractAllScripts: Extract all JSON-LD from HTML documents
  • processingMode: JSON-LD version compatibility mode

from_rdf() Options

  • format: Input serialization format (currently supports 'application/n-quads')
  • useRdfType: Keep rdf:type as property instead of converting to @type
  • useNativeTypes: Convert XSD datatypes to Python native types

RDF Parser Registration

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

Example

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'})

Data Type Mapping

XSD to Python Type Conversion

When useNativeTypes is True, XSD datatypes are converted to Python types:

XSD TypePython TypeExample
xsd:booleanbooltrueTrue
xsd:integerint"42"42
xsd:doublefloat"3.14"3.14
xsd:stringstr"hello""hello"

Blank Node Handling

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

Error Handling

RDF conversion functions may raise JsonLdError with these specific error types:

  • invalid RDF: Malformed RDF input
  • RDF parsing error: Parser-specific errors
  • invalid JSON-LD: JSON-LD that cannot be converted to RDF
  • loading document failed: Issues loading remote RDF documents

Use Cases

Triple Store Integration

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

Data Pipeline Processing

# 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

docs

core-processing.md

document-loading.md

index.md

json-canonicalization.md

rdf-conversion.md

url-utilities.md

tile.json