Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in JSON.stringify result between browsers

When I JSON.stringify() the following code:

var exampleObject = { "name" : "Žiga Kovač", "kraj" : "Žužemberk"};

I get different results between browsers.

IE8 and Google Chrome return:

{"name":"\u017diga Kova\u010d","kraj":"\u017du\u017eemberk"}

While Firefox and Opera return:

{"name":"Žiga Kovač","kraj":"Žužemberk"}

I am using the browser's native JSON implementation in all 4 browsers. If I undefine the native JSON implementation and replace it with the one from json.org, then all browsers return:

{"name":"Žiga Kovač","kraj":"Žužemberk"}

Why is this happening, which result is correct and is it possible to make that all browsers return:

{"name":"\u017diga Kova\u010d","kraj":"\u017du\u017eemberk"}

?

like image 229
Matic Avatar asked Oct 05 '10 09:10

Matic


People also ask

What is the difference between JSON Stringify and JSON parse?

parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string.

What happens when you JSON Stringify a string?

JSON.stringify() converts a value to JSON notation representing it: Boolean , Number , String , and BigInt (obtainable via Object() ) objects are converted to the corresponding primitive values during stringification, in accordance with the traditional conversion semantics.

Is JSON parse opposite of JSON Stringify?

JSON. stringify() is the opposite of JSON. parse(), which converts JSON into Javascript objects.

Is JSON Stringify safe?

By itself, the use of JSON. stringify() is fine. Using the output of JSON. stringify() in a JavaScript context will result in the expected behavior.


2 Answers

These two representations are absolutely equivalent.

The one uses Unicode escape sequences (\uxxxx) to represent a Unicode character, the other uses an actual Unicode character. json.org defines a string as:

string
    - ""
    - "chars"
chars
    - char
    - char chars
char
    - any Unicode character except " or \ or control characters
    - one of: \" \\ \/ \b \f \n \r \t
    - \u four-hex-digits

There is no difference in the strings themselves, only in their representation. This is the same thing HTML does when you use ©, © or © to represent the copyright sign.

like image 81
Tomalak Avatar answered Oct 19 '22 10:10

Tomalak


The 'correct' (visibly) version is a UTF8 string, and the escaped string is an ASCII string with UTF8 escape codes. While the first one can be used in an HTTP body (as long as content-encoding is set to UTF8), the second one can also be used in an HTTP GET request header.

If you want to use the UTF8 version in a GET request, you need to escape it first, using encodeURIComponent.

When the content is received on the server side, the native string implementation will make sure that it contains exactly the same data (from all clients), provided that the HTTP transmission is correct.

Your browser will generally handle the encoding of it, if you send it as an HTTP POST body.

like image 29
Tor Valamo Avatar answered Oct 19 '22 12:10

Tor Valamo