JSON Reference and Pointer Utilities
json-refs is a JavaScript library providing comprehensive utilities for interacting with JSON References (based on draft-pbryan-zyp-json-ref-03) and JSON Pointers (RFC6901). Its primary function is to resolve references within JSON documents, making it highly valuable for managing complex data structures such as OpenAPI/Swagger definitions, JSON Schemas, or any document with interlinked parts. The current stable version is 3.0.15. While a strict release cadence is not explicitly defined, the project shows active maintenance, including significant breaking changes in v3.0.0 and critical security patches in v2.1.7. A key differentiator is its dual focus on both JSON Reference and JSON Pointer specifications, offering robust solutions for Node.js and browser environments, complete with TypeScript type definitions.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'resolveRefs') OR TypeError: jsonRefs.resolveRefs is not a function
cause Incorrect import statement for a CommonJS module in an environment expecting ESM, or attempting to destructure from the main export.fixFor TypeScript or modern JavaScript with transpilation, use `import jsonRefs from 'json-refs';`. For pure CommonJS, use `const jsonRefs = require('json-refs');`. Do not use named imports like `import { resolveRefs } from 'json-refs';`. -
Error: Could not resolve reference: ... (followed by a path)
cause The specified JSON reference path is invalid, the referenced document cannot be found, or `options.location` is not correctly configured for relative references, especially after upgrading to v3.0.0.fixVerify the JSON Pointer path within the reference. Ensure all external files or URLs are accessible. If using relative paths, set `options.location` to the base URI of the document containing the reference. Remember `options.relativeBase` was removed in v3.0.0. -
Circular reference detection (or lack thereof) leading to unexpected output or infinite loops.
cause Behavior for circular reference resolution changed in v3.0.0. The default `options.resolveCirculars` is `false`, meaning circular references are handled differently than in previous versions.fixExplicitly set `options.resolveCirculars: true` if you intend to fully resolve all circular references, but be cautious of potential performance or output size issues. Otherwise, understand that circular paths might not be fully resolved by default.
Warnings
- breaking The `options.relativeBase` property has been removed. Users should now use `options.location` to specify the base URI for resolving relative references.
- breaking The behavior for resolving circular references has changed significantly. Previously, all references to circular paths were unresolved. Now, if `options.resolveCirculars` is `false` (which is the default), references to circular paths will only be resolved if the reference location itself is not circular.
- breaking Versions prior to `v2.1.7` contained security vulnerabilities due to insecure versions of internal dependencies (`qs` and `uri-js`) used by `path-loader`. These issues could potentially lead to denial of service or other exploits.
Install
-
npm install json-refs -
yarn add json-refs -
pnpm add json-refs
Imports
- jsonRefs
const jsonRefs = require('json-refs');import jsonRefs from 'json-refs';
- resolveRefs
import { resolveRefs } from 'json-refs';import jsonRefs from 'json-refs'; jsonRefs.resolveRefs(root, options);
- findRefs
const findRefs = require('json-refs').findRefs;import jsonRefs from 'json-refs'; jsonRefs.findRefs(root, options);
Quickstart
import jsonRefs from 'json-refs';
const documentWithRefs = {
a: 'Hello',
b: { $ref: '#/a' },
c: { $ref: 'external.json#/message' },
d: { $ref: 'https://example.com/api/spec.json#/paths/~1users' }
};
const options = {
// For demonstration, mock a simple external file.
// In a real application, you'd use a loader for 'external.json'.
loaderOptions: {
processContent: function (res, callback) {
if (res.url.endsWith('external.json')) {
callback(null, {
text: JSON.stringify({ message: 'World from external!' })
});
} else {
callback(null, res.text);
}
}
},
// Important for resolving relative references like 'external.json'
location: 'file:///path/to/base/document.json' // Replace with actual base URI if needed
};
jsonRefs.resolveRefs(documentWithRefs, options)
.then(function (res) {
console.log('Resolved Document:', JSON.stringify(res.resolved, null, 2));
console.log('Unresolved References:', res.unresolved);
})
.catch(function (err) {
console.error('Error resolving references:', err);
});