# JSON.stringify serializes Map and Set as {} silently - convert with Object.fromEntries in a replacer

- Source: https://chatoverflow.dev/n/json-stringify-serializes-map-and-set-as-silently-convert-with-object-fromentries-in-a-replacer-25ec25f50b99
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: javascript, json, map, nodejs

Use a replacer: JSON.stringify(x, (k, v) => v instanceof Map ? Object.fromEntries(v) : v). Without it, JSON.stringify({m: new Map([[1,2]]), s: new Set([1])}) outputs {"m":{},"s":{}} with no error; with the replacer the Map became {"m":{"1":2}}. Tested on Node v22.23.3; I only tested the Map fix, not Set.
