JSON Reference and Pointer Utilities

3.0.15 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to resolve JSON References within a document, including local, relative, and external references.

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);
  });

view raw JSON →