Skip to content

jsonld_serialization

JSON-LD is “A JSON-based Serialization for Linked Data” (https://www.w3.org/TR/json-ld/) that RDFLib implements for RDF serialization.

This file demonstrated some of the JSON-LD things you can do with RDFLib. Parsing & serializing so far. More to be added later.

Parsing

There are a number of “flavours” of JSON-LD - compact and verbose etc. RDFLib can parse all of these in a normal RDFLib way.

Serialization

JSON-LD has a number of options for serialization - more than other RDF formats. For example, IRIs within JSON-LD can be compacted down to CURIES when a “context” statement is added to the JSON-LD data that maps identifiers - short codes - to IRIs and namespace IRIs like this:

.. code-block:: json

"@context": {
    "dcterms": "http://purl.org/dc/terms/",
    "schema": "https://schema.org/"
}

Here the short code “dcterms” is mapped to the IRI http://purl.org/dc/terms/ and “schema” to https://schema.org/, as per RDFLib’s in-build namespace prefixes.

Attributes:

context module-attribute

context = {'sdo': 'https://schema.org/', 'dct': 'http://purl.org/dc/terms/'}

g module-attribute

g = parse(data='\n    PREFIX dcterms: <http://purl.org/dc/terms/>\n    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n\n    <http://example.com/person/nick>\n        a dcterms:Agent ;\n        <https://schema.org/name> "Nicholas Car" ;\n        <https://schema.org/memberOf> <https://kurrawong.ai> ;\n    .\n\n    <https://kurrawong.ai>\n        a dcterms:Agent , <https://schema.org/Organization> ;\n        <https://schema.org/name> "KurrawongAI" ;\n    .\n    ')

json_ld_data_string module-attribute

json_ld_data_string = '\n{\n  "@context": {\n    "dct": "http://purl.org/dc/terms/",\n    "sdo": "https://schema.org/"\n  },\n  "@graph": [\n    {\n      "@id": "https://kurrawong.ai",\n      "@type": [\n        "dct:Agent",\n        "sdo:Organization"\n      ],\n      "sdo:name": "KurrawongAI"\n    },\n    {\n      "@id": "http://example.com/person/nick",\n      "@type": "dct:Agent",\n      "sdo:memberOf": {\n        "@id": "https://kurrawong.ai"\n      },\n      "sdo:name": "Nicholas Car"\n    }\n  ]\n}\n'