15

I have a simple JSON where number is not getting parsed properly.

[
  {
    "orderNumber": 1,
    "customerId": 228930314431312345,
    "shoppingCartId": 22893031443137109,
    "firstName": "jjj"
  }
]

I tried it @ http://www.utilities-online.info/xmltojson/ and the result was

<?xml version="1.0" encoding="UTF-8" ?>
<orderNumber>1</orderNumber>
<customerId>228930314431312350</customerId>
<shoppingCartId>22893031443137108</shoppingCartId>
<firstName>jjj</firstName>

As you can see....the XML is different from JSON. I'm new to JSON. Am I missing something?

MikO
  • 18,243
  • 12
  • 77
  • 109
Jeet
  • 311
  • 1
  • 4
  • 21

2 Answers2

15

This is a Javascript precision problem.

According to Mozilla Developer Network:

ECMA-262 only requires a precision of up to 21 significant digits. Other implementations may not support precisions higher than required by the standard.

Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toPrecision

I pasted your array into Google Chrome's Javascript console and got back this: Javascript precision bug

So it looks like Javascript is rounding the values before they are being converted to XML. Since your conversion is being done via Javascript in the browser at http://www.utilities-online.info/xmltojson/, it makes sense why the number was changed.

(Note: I tested on Google Chrome version 26.0.1410.43 m using Windows 7 Professional)

Edit:

Is there any reason why you cannot pass these values to Javascript as strings?

Try this:

[
  {
    "orderNumber": "1",
    "customerId": "228930314431312345",
    "shoppingCartId": "22893031443137109",
    "firstName": "jjj"
  }
]

I was able to do this and save the values successfully. However, you will not be able to run a math calculation on them in Javascript without losing some precision, unless you are doing something like multiplying by 0, of course.

Javascript precision string workaround

This also converted to XML correctly using your reference http://www.utilities-online.info/xmltojson/.

Aiias
  • 4,683
  • 1
  • 18
  • 34
  • I had tried on IE 9 and Chrome. Basically, I am trying to get a JSON response from my Spring MVC controller and am getting invalid values. I used the XML converter just to show request/response. Thanks! I tried gson and jackson. – Jeet Apr 08 '13 at 03:46
  • @Jeet - See my **Edit**. I have found a workaround that may work for your purposes. – Aiias Apr 08 '13 at 03:53
  • Well..I can pass them as strings, but wouldn't that be manual intervention? I was hoping for an out of the box solution. – Jeet Apr 08 '13 at 06:27
  • @Jeet - The moment you create that large value as **Numeric** in Javascript, it will lose precision. – Aiias Apr 08 '13 at 06:32
  • @Jeet - How are you generating your JSON? – Aiias Apr 08 '13 at 06:41
  • I am making an Ajax call to Controller which is returing a model object with these IDs. I tried Jackson to convert it automatically and then tried gson to convert to json and return String. ResponseBody (with Jackson) and ResponseBody String with gson. – Jeet Apr 08 '13 at 14:54
5

Javascript represents its numbers as double precision floats which limits the largest integer number that can be represented to +-9007199254740992. Here is the ECMA documentation.

David Weber
  • 1,965
  • 1
  • 22
  • 32