Require JSON files with a file extension other than .json
.
$ npm install @chharvey/requirejson
const {requireJSON} = require('@chharvey/requirejson');
requireJSON('./my-file.jsonld').then((my_json_object) => {
console.log(my_json_object['@type']);
});
const {requireJSONSync} = require('@chharvey/requirejson');
const my_json_object = requireJSONSync('./my-file.jsonld');
console.log(my_json_object['@type']);
import {
requireJSON,
requireJSONSync,
JSONValue,
JSONObject,
JSONArray,
JSONPrimitive,
} from '@chharvey/requirejson';
const my_json_object: Promise<JSONValue> = requireJSON('./my-file.jsonld');
const my_json_object_sync: JSONValue = requireJSONSync('./my-file.jsonld');
Asynchronously returns a JSON value that is the result of parsing the file contents.
filepath
: the path of the file to read, relative to current working directory
A Promise resolving to a JSON value parsed from the file contents.
Synchronously returns a JSON value that is the result of parsing the file contents.
filepath
: the path of the file to read, relative to current working directory
A JSON value parsed from the file contents.
type name | definition | description |
---|---|---|
JSONValue | JSONObject or JSONArray or JSONPrimitive |
Any valid JSON value. |
JSONObject | {[key: string]?: JSONValue} |
A general JSON object, with string keys and JSONValue values. |
JSONArray | JSONValue[] |
A JSON array, with JSONValue entries. |
JSONPrimitive | string or number or boolean or null |
A JSON primitive. |