JSON.stringify
was previously specified to return ill-formed Unicode strings if the input contains any lone surrogates:
JSON.stringify('\uD800');
// → '"�"'
The “well-formed JSON.stringify
” proposal changes JSON.stringify
so it outputs escape sequences for lone surrogates, making its output valid Unicode (and representable in UTF-8):
JSON.stringify('\uD800');
// → '"\\ud800"'
Note that JSON.parse(stringified)
still produces the same results as before.
This feature is a small fix that was long overdue in JavaScript. It’s one less thing to worry about as a JavaScript developer. In combination with JSON ⊂ ECMAScript, it enables safely embedding JSON-stringified data as literals into JavaScript programs, and writing the generated code to disk in any Unicode-compatible encoding (e.g. UTF-8). This is super useful for metaprogramming use cases.
Feature support #
- Chrome: supported since version 72
- Firefox: supported since version 64
- Safari: supported since version 12.1
- Node.js: supported since version 12
- Babel: supported