Well-formed JSON.stringify

Published · Tagged with ECMAScript ES2019

JSON.stringify was previously specified to return ill-formed Unicode strings if the input contains any lone surrogates:

// → '"�"'

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):

// → '"\\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 #