npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

json-tolerant-reader

v1.0.0

Published

A lightweight utility for recursively searching and extracting values from nested JSON structures by key name. Inspired by Martin Fowler's Tolerant Reader pattern.

Readme

json-tolerant-reader

A lightweight utility for recursively searching and extracting values from nested JSON structures by key name. Inspired by Martin Fowler's Tolerant Reader pattern.

Why Tolerant Reader?

"Be conservative in what you do, be liberal in what you accept from others." — Jon Postel

The Tolerant Reader is an approach to building robust integrations that can handle changes in json objects. This library embodies that principle by:

  • Ignoring unknown fields: Only searches for the specific key you need
  • Handling structural changes: Works regardless of nesting depth or structure
  • Graceful degradation: Returns null instead of throwing errors when keys are missing
  • Flexible extraction: Can find values anywhere in the structure without requiring exact paths

This makes your code more resilient to API changes, schema evolution, and varying data formats.

Real-World Example: Resilient API Integration

Imagine you're consuming a third-party API that returns user data. Your code expects to find an email field:

const jtr = require('json-tolerant-reader')

// Original API response (v1)
const apiResponseV1 = {
  user: {
    id: 123,
    email: '[email protected]',
    profile: {
      name: 'John Doe'
    }
  }
}

// Your code works fine
const email = jtr(apiResponseV1, 'email')  // '[email protected]'

Later, the API changes its structure (v2), moving email into a nested contact object:

// New API response (v2) - structure changed!
const apiResponseV2 = {
  user: {
    id: 123,
    profile: {
      name: 'John Doe',
      contact: {
        email: '[email protected]',
        phone: '+1234567890'
      }
    }
  }
}

// Your code still works! No changes needed
const email = jtr(apiResponseV2, 'email')  // '[email protected]'

Without json-tolerant-reader, you'd need to update your code:

// Brittle approach - breaks when API changes
const emailV1 = apiResponseV1.user.email           // Works in v1
const emailV2 = apiResponseV2.user.email           // undefined in v2 ❌
const emailV2Fixed = apiResponseV2.user.profile.contact.email  // Now you need this

With json-tolerant-reader, your code continues to work regardless of where email is located in the structure. This makes your integration resilient to API changes, refactoring, and schema evolution.

Installation

npm install json-tolerant-reader

Usage

const jtr = require('json-tolerant-reader')

const data = {
  name: 'John Doe',
  address: {
    city: 'New York',
    coordinates: {
      lat: 40.76,
      long: -73.97
    }
  },
  work: {
    address: {
      city: 'Boston'
    }
  }
}

// Find first occurrence
jtr(data, 'city')  // 'New York'

// Find all occurrences
jtr(data, 'city', true)  // ['New York', 'Boston']

// Find nested values
jtr(data, 'lat')  // 40.76

// Returns null if not found
jtr(data, 'unknown')  // null

API

jsonTolerantReader(json, jsonKey, allOccurrences)

Recursively searches for a key in a JSON object or array.

Parameters:

  • json (Object|Array) - The JSON object or array to search
  • jsonKey (string) - The key name to search for
  • allOccurrences (boolean, optional) - If true, returns all matching values; if false (default), returns only the first match

Returns:

  • When allOccurrences is false: The first matching value, or null if not found
  • When allOccurrences is true: An array of all matching values, or [] if none found

License

MIT License