zino
zino

Reputation: 1472

Does JSON.stringify return the same string on all modern JS engines?

Can I compare two identical data-only objects from two different JS engines that are converted to strings using JSON.stringify?

Will they have different amounts of white space/escape characters/orders, or be identical character for character?

Does the JS language spec define a exact standard for JSON.stringify'd data?

Im only interested in modern JS engines (2016+).

Upvotes: 1

Views: 1384

Answers (2)

Vad
Vad

Reputation: 4099

You can use fastest-stable-stringify to predictably stringify your JSON.

Upvotes: -1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196177

According to MDN (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)

JSON.stringify() converts a value to JSON notation representing it:

  • Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.

Upvotes: 2

Related Questions