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
. - Term expansion with the
ContextParser.expandTerm
helper function.
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.
// Expands if `name` is present in the context
ContextParser.expandTerm('name', 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);
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.