A comprehensive JSON-LD Processor and API implementation in JavaScript for processing Linked Data in JSON format
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Bi-directional conversion between JSON-LD and RDF datasets, including canonicalization for cryptographic applications. These operations enable JSON-LD to work seamlessly with RDF data and provide cryptographically secure document canonicalization.
Converts JSON-LD to RDF dataset format, optionally serializing to N-Quads format.
/**
* Converts JSON-LD to RDF dataset
* @param input - The JSON-LD input
* @param options - Optional configuration object
* @returns Promise resolving to RDF dataset or N-Quads string
*/
function toRDF(input, options);Parameters:
input (any): JSON-LD input to convertoptions (ToRdfOptions, optional): Configuration optionsOptions:
base (string): Base IRI to use (default: input URL if string, empty string otherwise)expandContext (any): Context to expand withskipExpansion (boolean): Skip expansion step (default: false)format (string): Output format: 'application/n-quads' for N-Quads stringproduceGeneralizedRdf (boolean): Output generalized RDF (default: false)documentLoader (function): Custom document loadersafe (boolean): Use safe mode (default: false)rdfDirection (string): Support for @direction: null or 'i18n-datatype' (default: null)Usage Examples:
const jsonld = require('jsonld');
// Convert to RDF dataset
const doc = {
"@context": {"name": "http://schema.org/name"},
"@id": "http://example.org/people/jane",
"name": "Jane Doe"
};
const dataset = await jsonld.toRDF(doc);
// Result: RDF dataset object
// Convert to N-Quads string
const nquads = await jsonld.toRDF(doc, {format: 'application/n-quads'});
// Result: "<http://example.org/people/jane> <http://schema.org/name> \"Jane Doe\" .\n"
// Convert with options
const rdf = await jsonld.toRDF(doc, {
base: 'http://example.org/',
produceGeneralizedRdf: true
});Converts RDF dataset to JSON-LD format, supporting various RDF serialization formats.
/**
* Converts RDF dataset to JSON-LD
* @param dataset - RDF dataset or serialized string
* @param options - Optional configuration object
* @returns Promise resolving to JSON-LD document
*/
function fromRDF(dataset, options);Parameters:
dataset (any): RDF dataset to convert or serialized stringoptions (FromRdfOptions, optional): Configuration optionsOptions:
format (string): Input format if dataset is string: 'application/n-quads' (default for strings)rdfParser (function): Custom RDF parser to useuseRdfType (boolean): Use rdf:type instead of @type (default: false)useNativeTypes (boolean): Convert XSD types to native JavaScript types (default: false)rdfDirection (string): Support for @direction: null or 'i18n-datatype' (default: null)safe (boolean): Use safe mode (default: false)Usage Examples:
// Convert from N-Quads string
const nquads = '<http://example.org/people/jane> <http://schema.org/name> "Jane Doe" .';
const doc = await jsonld.fromRDF(nquads, {format: 'application/n-quads'});
// Result: JSON-LD document
// Convert from RDF dataset object
const dataset = [
{
subject: {termType: 'NamedNode', value: 'http://example.org/people/jane'},
predicate: {termType: 'NamedNode', value: 'http://schema.org/name'},
object: {termType: 'Literal', value: 'Jane Doe'},
graph: {termType: 'DefaultGraph', value: ''}
}
];
const doc2 = await jsonld.fromRDF(dataset);
// Convert with native types
const doc3 = await jsonld.fromRDF(nquads, {
format: 'application/n-quads',
useNativeTypes: true,
useRdfType: false
});Performs RDF dataset normalization using canonicalization algorithms, essential for cryptographic applications and document comparison.
/**
* Performs RDF dataset normalization/canonicalization
* @param input - Input to normalize (JSON-LD or other format)
* @param options - Optional configuration object
* @returns Promise resolving to normalized output
*/
function normalize(input, options);
/**
* Alias for normalize
*/
function canonize(input, options);Parameters:
input (any): Input to normalize as JSON-LD or specified formatoptions (NormalizeOptions, optional): Configuration optionsOptions:
algorithm (string): Normalization algorithm: 'URDNA2015' or 'URGNA2012' (default: 'URDNA2015')base (string): Base IRI to use (default: null for safety)expandContext (any): Context to expand withskipExpansion (boolean): Skip expansion step (default: false)inputFormat (string): Input format if not JSON-LD: 'application/n-quads'format (string): Output format: 'application/n-quads' for N-Quads stringdocumentLoader (function): Custom document loaderuseNative (boolean): Use native canonize algorithm if availablerdfDirection (string): Support for @direction: null or 'i18n-datatype' (default: null)safe (boolean): Use safe mode (default: true, different from other operations)Usage Examples:
// Basic canonicalization
const doc = {
"@context": {"name": "http://schema.org/name"},
"name": "Jane Doe"
};
const canonized = await jsonld.canonize(doc, {
algorithm: 'URDNA2015',
format: 'application/n-quads'
});
// Result: Canonical N-Quads string
// Canonicalize N-Quads input
const nquads = '<http://example.org/s> <http://example.org/p> "value" .';
const canonical = await jsonld.normalize(nquads, {
inputFormat: 'application/n-quads',
format: 'application/n-quads'
});
// Canonicalize for digital signing (recommended safe settings)
const safeCanonical = await jsonld.canonize(doc, {
algorithm: 'URDNA2015',
format: 'application/n-quads',
safe: true,
base: null
});Register custom RDF parsers for additional RDF serialization formats.
/**
* Registers RDF dataset parser by content-type
* @param contentType - Content type for the parser
* @param parser - Parser function
*/
function registerRDFParser(contentType, parser);
/**
* Unregisters RDF dataset parser by content-type
* @param contentType - Content type for the parser
*/
function unregisterRDFParser(contentType);Usage Examples:
// Register synchronous RDF parser
jsonld.registerRDFParser('application/turtle', input => {
// Parse Turtle input to RDF dataset object
return parseToDataset(input);
});
// Register asynchronous RDF parser
jsonld.registerRDFParser('application/rdf+xml', async input => {
// Parse RDF/XML input to RDF dataset object
return await parseRdfXmlToDataset(input);
});
// Unregister parser
jsonld.unregisterRDFParser('application/turtle');/**
* Options for toRDF operations
*/
interface ToRdfOptions extends JsonLdOptions {
format?: 'application/n-quads' | 'application/nquads';
produceGeneralizedRdf?: boolean;
rdfDirection?: null | 'i18n-datatype';
skipExpansion?: boolean;
}
/**
* Options for fromRDF operations
*/
interface FromRdfOptions {
format?: 'application/n-quads' | 'application/nquads';
rdfParser?: (input: string) => any;
useRdfType?: boolean;
useNativeTypes?: boolean;
rdfDirection?: null | 'i18n-datatype';
safe?: boolean;
}
/**
* Options for normalization/canonicalization operations
*/
interface NormalizeOptions extends JsonLdOptions {
algorithm?: 'URDNA2015' | 'URGNA2012';
inputFormat?: 'application/n-quads' | 'application/nquads';
format?: 'application/n-quads' | 'application/nquads';
useNative?: boolean;
rdfDirection?: null | 'i18n-datatype';
skipExpansion?: boolean;
}
/**
* RDF parser function type
*/
type RDFParser = (input: string) => any | Promise<any>;
/**
* RDF dataset quad structure
*/
interface Quad {
subject: NamedNode | BlankNode;
predicate: NamedNode;
object: NamedNode | BlankNode | Literal;
graph: NamedNode | BlankNode | DefaultGraph;
}
/**
* RDF term types
*/
interface NamedNode {
termType: 'NamedNode';
value: string;
}
interface BlankNode {
termType: 'BlankNode';
value: string;
}
interface Literal {
termType: 'Literal';
value: string;
language?: string;
datatype?: NamedNode;
}
interface DefaultGraph {
termType: 'DefaultGraph';
value: '';
}