Small utility · Precise result

Find the key.
Keep the value.

Recursively search for keys inside JSON objects and retrieve their values.

npm install key-searcher
View on npm
  • MIT
  • zero dependencies
  • ESM
  • 3.3 kB unpacked (npm unpacked size)

Growth story

2,363 downloads settled through Aug 10, 2026 · plus 1,273 provisional on Aug 11
Goal 10k daily downloads
All-time settled peak
526 · Aug 10, 2026
Before this window
488 downloads

Daily activity chart, Jun 13–Aug 11, 2026. Linear, zero-based scale; bars show magnitude; dots mark days with activity too small to draw. The 1,875-download window includes 1,273 provisional downloads on Aug 11. Baked npm registry snapshot through Aug 11, 2026.

Daily npm downloads for key-searcher from 2026-06-13 through 2026-08-11, followed by unwindowed context totals
Date or contextDownloads
2026-06-131
2026-06-1410
2026-06-150
2026-06-160
2026-06-170
2026-06-182
2026-06-190
2026-06-200
2026-06-214
2026-06-221
2026-06-230
2026-06-240
2026-06-252
2026-06-260
2026-06-274
2026-06-280
2026-06-290
2026-06-307
2026-07-010
2026-07-026
2026-07-032
2026-07-041
2026-07-051
2026-07-064
2026-07-070
2026-07-080
2026-07-098
2026-07-100
2026-07-114
2026-07-120
2026-07-130
2026-07-140
2026-07-151
2026-07-160
2026-07-170
2026-07-180
2026-07-190
2026-07-200
2026-07-210
2026-07-220
2026-07-231
2026-07-241
2026-07-258
2026-07-260
2026-07-270
2026-07-281
2026-07-290
2026-07-300
2026-07-310
2026-08-010
2026-08-020
2026-08-030
2026-08-040
2026-08-051
2026-08-060
2026-08-075
2026-08-080
2026-08-091
2026-08-10526
2026-08-11 (provisional)1,273
Before displayed window488
Lifetime total2,363

Live playground

Search the structure.

Edit the strict JSON and choose the keys to retrieve. The result updates as you type and preserves matched subtrees.

Input Strict JSON
Result 3 requested

Result updated.

Works in the browser. Zero dependencies.

On this page

API reference

One function, precisely documented.

The package exposes one default ESM export. These are the observed v1.0.3 semantics.

searchIn(source, ...keys) — default export, ESM

Parameters

  • sourceobject or array; required.
  • ...keysstrings; zero or more accepted.

Return value

A new object whose own properties are the requested keys. Matched values are copied by reference; missing keys have value undefined.

Throws

TypeError for null or primitive input. RangeError may occur for circular references or inputs deep enough to exhaust the call stack.

Accepted domain

Objects and arrays containing nested objects or arrays. Key arguments are strings.

Normal example

searchIn({ user: { id: 7 } }, "id")
// { id: 7 }

Boundary example

const out = searchIn({ ok: true }, "missing")
Object.keys(out) // ["missing"]
out.missing      // undefined
JSON.stringify(out) // "{}"

Install & import

Choose a package manager.

npm install key-searcher

ESM only — require() is not supported.

import searchIn from "key-searcher";

No bundled TypeScript declarations. npm metadata reports types: null.

Behavior & limitations

Observable facts, including the sharp edges.

Measured against v1.0.3. These notes are here so you can decide whether the function fits your input before shipping it.

1

Missing keys remain present

A key not found is present with value undefined. Object.keys() includes it; JSON.stringify merely hides it.

2

Returned subtrees share references

The result object is new, but matched objects are the original references. Mutating a returned subtree mutates the source. The search itself does not mutate the input.

3

Circular references can throw

When the key is not matched before a cycle re-enters, recursive traversal exhausts the stack and throws RangeError.

4

The shallowest match wins

A duplicate key deeper in the tree is discarded. {n:{dup:"L2"}, dup:"L1"} resolves dup to "L1".

5

No input validation

null and primitive input throw TypeError (“Cannot use 'in' operator”).

6

Arrays are traversed

Keys inside array elements are found.

7

Depth is bounded by the stack

Very deep inputs may exhaust the call stack because traversal is recursive.

Current version 1.0.3 · published 2026-03-23 · MIT · 3.3 kB unpacked (npm unpacked size)npmSourceIssuesLicence

Features

Package features

Recursive search

Traverses objects recursively to find matching keys.

Finds keys at any depth

Matches keys wherever they are nested.

Preserves matched subtrees

Keeps the entire object beneath a matched key.

Read-only search

The search itself does not mutate the input; returned subtrees still share source references.

Example

One import. One call.

key-searcher is ESM-only and exposes a single default export.

import searchIn from "key-searcher";

const obj1 = {
  key1: false,
  key2: {
    key3: "val2",
    key4: {
      key5: "val3",
      key6: { key7: "val4", key8: { key9: "val5" } },
    },
    key5: "val4",
  },
  key10: { key11: "val6" },
};

const newJSON = searchIn(obj1, "key25", "key1", "key6");
// { key25: undefined, key1: false, key6: { key7: 'val4', key8: { key9: 'val5' } } }

Use cases

Ways to use key-searcher

Filtering API responses

Keep only the fields needed from noisy responses.

Extracting deeply nested fields

Pull values from complex JSON structures.

Cleaning large JSON structures

Return only the requested keys and their values.