Symbol Keys Hide From Object.keys And JSON
Symbol property keys stay invisible to Object.keys, for..in and JSON.stringify, and only getOwnPropertySymbols or Reflect.ownKeys can enumerate them.
Code
// Symbol property keys are invisible to every enumeration mechanism by default: Object.keys, for..in, and JSON.stringify all miss them
const id = Symbol('id');
const record = { name: 'adapter', [id]: 42 };
console.log('Object.keys:', JSON.stringify(Object.keys(record)));
console.log('JSON.stringify:', JSON.stringify(record));
// Listing Symbol keys explicitly: only these two APIs can see them
console.log(
'own symbols:',
Object.getOwnPropertySymbols(record)
.map((s) => s.description)
.join(','),
);
console.log('Reflect.ownKeys entries:', Reflect.ownKeys(record).length);
// This is why libraries hang internal metadata on Symbols: serialization never leaks it
record[id] = 43;
console.log('symbol value still readable:', record[id]);Output
Captured from a sandboxed run of this snippet in the build pipeline.
Object.keys: ["name"]
JSON.stringify: {"name":"adapter"}
own symbols: id
Reflect.ownKeys entries: 2
symbol value still readable: 43How it works
Top-level bindings id, record hold the demo state — nothing sits inside a function scope.
Module-level object literal, fixed keys: name.
Applying it
The run emits 5 labeled sections — Object.keys:, JSON.stringify:, own symbols:, Reflect.ownKeys entries:, symbol value still readable: — one per step.
Statements that matter
.map((s) => s.description)
.join(','),
);
Interactive Tools for This Topic
CSV to JSON ConverterConvert comma-separated values into a JSON array of objects using the first row as field names, with support for quoted and escaped fields.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON Diff ViewerCompare two JSON documents and list every added, removed, and changed path in a structural diff, with clear before and after values.
FAQ
- Is anything beyond the standard library required?
- Nothing beyond the standard library — the snippet is dependency-free. Its state lives in module-level bindings: `id`, `record`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.