https://github.com/doga/rdf-json-parser
An RDF-JSON parser.
https://github.com/doga/rdf-json-parser
rdf
Last synced: about 1 month ago
JSON representation
An RDF-JSON parser.
- Host: GitHub
- URL: https://github.com/doga/rdf-json-parser
- Owner: doga
- License: apache-2.0
- Created: 2025-03-07T09:20:55.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-28T14:35:13.000Z (about 2 months ago)
- Last Synced: 2025-03-28T15:34:23.438Z (about 2 months ago)
- Topics: rdf
- Language: JavaScript
- Homepage: https://www.w3.org/TR/rdf-json/
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# An RDF-JSON parser
A JavaScript library for parsing [RDF-JSON](https://www.w3.org/TR/rdf-json/).
## Usage examples
Parsing and serialising
description = '''
Transform valid RDF-JSON into an RDF dataset, and back again.
'''```javascript
import { parse, serialise } from 'https://esm.sh/gh/doga/[email protected]/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:
```text
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.
'''```javascript
import { parse, serialise } from 'https://esm.sh/gh/doga/[email protected]/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:
```text
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](https://deno.com/) 2+):
```shell
deno run --allow-net --allow-run --allow-env --allow-read jsr:@andrewbrey/[email protected] --dax=false --mode=isolated https://raw.githubusercontent.com/doga/rdf-json-parser/refs/heads/main/README.md
```∎