Skip to content
This repository was archived by the owner on Apr 16, 2025. It is now read-only.

doga/rdf-json-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RDF logo

An RDF-JSON parser

A JavaScript library for parsing RDF-JSON.

Usage examples

Parsing and serialising
description = '''
Transform valid RDF-JSON into an RDF dataset, and back again.
'''
import { parse, serialise } from 'https://esm.sh/gh/doga/rdf-json-parser@1.1.1/mod.mjs';

const 
rdfJsonIn = {
  "http://site.example/id/Me" : {
    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : [
      { "value" : "http://xmlns.com/foaf/0.1/Person", "type" : "uri" }
    ]
  },
  "http://site.example" : {
    "http://purl.org/dc/terms/title" : [
      { "value" : "Anna's Homepage", "type" : "literal" },
      { "value" : "Annas hjemmeside", "type" : "literal", "lang" : "no"},
    ] 
  }
},
rdfDataset = parse(rdfJsonIn),
rdfJsonOut = serialise(rdfDataset);

console.group('Quads in the RDF dataset:');
for (const quad of rdfDataset) {
  console.group('Quad:');
  console.info(`Subject:   ${quad.subject.termType} "${quad.subject.value}".`);
  console.info(`Predicate: ${quad.predicate.termType} "${quad.predicate.value}".`);
  console.info(`Object:    ${quad.object.termType}   "${quad.object.value}".`);
  console.groupEnd();
}
console.groupEnd();

console.info('\nRDF-JSON serialisation of the RDF dataset:\n', rdfJsonOut);

Sample output for the code above:

Quads in the RDF dataset:
    Quad:
        Subject:   NamedNode "http://site.example/id/Me".
        Predicate: NamedNode "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".
        Object:    NamedNode   "http://xmlns.com/foaf/0.1/Person".
    Quad:
        Subject:   NamedNode "http://site.example".
        Predicate: NamedNode "http://purl.org/dc/terms/title".
        Object:    Literal   "Anna's Homepage".
    Quad:
        Subject:   NamedNode "http://site.example".
        Predicate: NamedNode "http://purl.org/dc/terms/title".
        Object:    Literal   "Annas hjemmeside".

RDF-JSON serialisation of the RDF dataset:
 {
  "http://site.example/id/Me": {
    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": [ { value: "http://xmlns.com/foaf/0.1/Person", type: "uri" } ]
  },
  "http://site.example": {
    "http://purl.org/dc/terms/title": [
      { value: "Anna's Homepage", type: "literal" },
      { value: "Annas hjemmeside", type: "literal", lang: "no" }
    ]
  }
}
Detecting invalid RDF-JSON
description = '''
Parsing an valid RDF-JSON fails.
'''
import { parse, serialise } from 'https://esm.sh/gh/doga/rdf-json-parser@1.1.1/mod.mjs';

const 
rdfJsonIn = {
  "http://site.example/id/Me": {
    "https://site.example/vcard": [{ value: "https://site.example/id/vcard/1234", type: "uri" }]
  },
  "https://site.example/id/vcard/1234": {
    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": [
      { value: "http://www.w3.org/2006/vcard/ns#Individual", type: "uri" }
    ],
    "http://www.w3.org/2006/vcard/ns#fn": [ { value: "Me", type: "literal" } ],
    "http://www.w3.org/2006/vcard/ns#hasTelephone": [
      { value: "https://site.example/id/vcard/1234/phone/1", type: "uri" }
    ]
  },
  "https://site.example/id/vcard/1234/phone/1": {
    "http://www.w3.org/2006/vcard/ns#hasValue": [
      { value: "tel:+41 22 738 73 59", type: "uri" } // invalid "tel:" URL contains spaces
    ]
  }
};

try {
  const rdfDataset = parse(rdfJsonIn);
  console.error(`Parsing should have failed.`);
} catch ( error ) {
  console.info(`Parsing failed: ${error}`);
}

Sample output for the code above:

Parsing failed: TypeError: Invalid NamedNode: IRI must not contain spaces.

Running the usage examples

Run the examples below by typing this in your terminal (requires Deno 2+):

deno run --allow-net --allow-run --allow-env --allow-read jsr:@andrewbrey/mdrb@3.0.4 --dax=false --mode=isolated https://raw.githubusercontent.com/doga/rdf-json-parser/refs/heads/main/README.md

About

[Archived] A parser for RDF-JSON, a format that is becoming obsolete due to its lack of RDF-Star support. Use this instead: https://github.com/doga/sparql-star-results

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors