Package Exports
- jsonld-context-parser
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonld-context-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
JSON-LD Context Parser
A JSON-LD @context
parser that will normalize these contexts so that they can easily be used in your application.
This parser has the following functionality:
- Fetch contexts by URLs.
- Normalize JSON contexts.
- Merge arrays of contexts.
- Create a default
@base
entry if a base IRI is provided. - Create
@id
entries for all@reverse
occurences. - Expand prefixes and
@vocab
in string values,@id
,@type
and@reverse
. - Context validation according to the JSON-LD specification while parsing (can be disabled).
- Term expansion with the
ContextParser.expandTerm
helper function.
Example input (with base IRI set to http://example.org/base
):
[
{
"@vocab": "http://vocab.org/",
"npmd": "https://linkedsoftwaredependencies.org/bundles/npm/",
"p": { "@id": "pred1", "@language": "nl" }
},
"http://example.org/simple.jsonld",
]
With X containing:
{
"xsd": "http://www.w3.org/2001/XMLSchema#",
"name": "http://xmlns.com/foaf/0.1/name"
}
Example output:
{
"@base": "http://example.org/base",
"@vocab": "http://vocab.org/",
"npmd": "https://linkedsoftwaredependencies.org/bundles/npm/",
"p": { "@id": "http://vocab.org/pred1", "@language": "nl" },
"xsd": "http://www.w3.org/2001/XMLSchema#",
"name": "http://xmlns.com/foaf/0.1/name"
},
Install
This package can be installed via npm.
$ npm install jsonld-context-parser
This package also works out-of-the-box in browsers via tools such as webpack and browserify.
Usage
API
Create a new parser
const ContextParser = require('jsonld-context-parse');
const myParser = new ContextParser();
Optionally, you can pass an options object with a custom document loader:
const myParser = new ContextParser({ documentLoader: myDocumentLoader });
Parse a context.
Either parse a context by URL:
const myContext = await myParser.parse('http://json-ld.org/contexts/person.jsonld');
by an non-normalized context:
const myContext = await myParser.parse({ ... });
or by an array of mixed contexts or URLs:
const myContext = await myParser.parse([
'http://json-ld.org/contexts/person.jsonld',
{ ... },
'https://linkedsoftwaredependencies.org/contexts/components.jsonld'
]);
Expand a term
Based on a context, terms can be expanded in vocab or base-mode.
Base expansion
Base expansion is done based on the @base
context entry.
This should typically be used for expanding terms in the subject or object position.
// Expands `person` based on the @base IRI. Will throw an error if the final IRI is invalid.
ContextParser.expandTerm('person', context);
// Expands if `foaf` is present in the context
ContextParser.expandTerm('foaf:name', context);
// Returns the URI as-is
ContextParser.expandTerm('http://xmlns.com/foaf/0.1/name', context);
Vocab expansion
Vocab expansion is done based on the @vocab
context entry.
This should typically be used for expanding terms in the predicate position.
// Expands `name` based on the @vocab IRI.
ContextParser.expandTerm('name', context, true);
// Expands if `foaf` is present in the context
ContextParser.expandTerm('foaf:name', context, true);
// Returns the URI as-is
ContextParser.expandTerm('http://xmlns.com/foaf/0.1/name', context, true);
Command-line
A command-line tool is provided to quickly normalize any context by URL, file or string.
Usage:
$ jsonld-context-parse url http://json-ld.org/contexts/person.jsonld
$ jsonld-context-parse file path/to/context.jsonld
$ jsonld-context-parse arg '{ "xsd": "http://www.w3.org/2001/XMLSchema#" }'
License
This software is written by Ruben Taelman.
This code is released under the MIT license.