451 words
2 minutes

JSON.parse Reviver Gets an Empty Key: Filter the Root After Parsing

2026-11-21
Frontend
JavaScript
/
Node.js
/
Troubleshooting
Contents

A JSON.parse() reviver receives "" for the final call on the parsed value. An actual property named "" arrives with the same key. If you want to keep only particular top-level fields, parse the payload first and apply the allowlist to the returned object. Its keys belong to the payload, so you can handle a real empty key explicitly.

Why the reviver changes more than the root#

ECMAScript’s JSON.parse algorithm puts the parsed value under an empty-string key on a temporary holder. InternalizeJSONProperty visits children before their containers and calls the reviver on that temporary holder last. Returning undefined deletes a child property. On the final call, returning undefined makes the entire parse return undefined. This happens after the JSON text has already parsed successfully. An unrelated JSON.stringify issue can also remove object properties, but that happens during serialization.

For this settings payload, the root allowlist should keep "" and settings, remove the root debug, and preserve settings.debug. A reviver that checks the key against that list happens to keep the root because the list includes "". It also strips the nested debug, since the callback applies at every depth.

Apply the allowlist to the parsed object#

Save the example as reviver.mjs and run node reviver.mjs. The assertions were verified on Node.js 24.13.0 on macOS arm64.

import assert from 'node:assert/strict'
const payload = '{"":"label","settings":{"debug":true},"debug":false}'
const calls = []
JSON.parse(payload, (key, value) => {
calls.push([key, typeof value])
return value
})
assert.deepEqual(calls, [
['', 'string'],
['debug', 'boolean'],
['settings', 'object'],
['debug', 'boolean'],
['', 'object'],
])
const allowed = new Set(['', 'settings'])
const recursive = JSON.parse(payload, (key, value) =>
allowed.has(key) ? value : undefined,
)
assert.deepEqual(recursive, { '': 'label', settings: {} })
const settingsOnlyReviver = (key, value) =>
key === 'settings' ? value : undefined
assert.equal(JSON.parse(payload, settingsOnlyReviver), undefined)
function pickRootFields(text, allowedKeys) {
const root = JSON.parse(text)
if (root === null || typeof root !== 'object' || Array.isArray(root)) {
throw new TypeError('Expected a top-level JSON object')
}
return Object.fromEntries(
Object.entries(root).filter(([key]) => allowedKeys.has(key)),
)
}
assert.deepEqual(pickRootFields(payload, allowed), {
'': 'label',
settings: { debug: true },
})
assert.deepEqual(pickRootFields(payload, new Set(['settings'])), {
settings: { debug: true },
})
for (const text of ['null', '[]', '42', '"label"']) {
assert.throws(() => pickRootFields(text, allowed), {
name: 'TypeError',
message: 'Expected a top-level JSON object',
})
}

The trace shows two calls with an empty key. The first one receives the actual label. The last one receives the entire document. Their types differ in this fixture, but a real empty-key property can contain an object too. Filtering after parsing avoids relying on either the key or the value’s type to identify the final callback.

The helper checks the root shape and copies selected fields at one level. If the settings contract also constrains values inside settings, validate that object separately.

References:

JSON.parse Reviver Gets an Empty Key: Filter the Root After Parsing
https://laplusda.com/en/posts/json-parse-reviver-empty-key-root/
Author
Zero
Published at
2026-11-21
License
CC BY-NC-SA 4.0