1

I'm struggling with parsing a JSON string containing really large numbers.

Consider the following JSON string:

const jsonString = `{
    "id": "eb2552e1673f4ad6bb70923e5920bbab",
    "Int32": 2147483647,
    "Int64": 9223372036854775807,
    "Int64Array": [9223372036854775807, 9223372036854775806],
    "Object": {
        "Int321": 2147483647,
        "Int641": 9223372036854775807,
        "Int64Array1": [9223372036854775807, 9223372036854775806]
    }
}`;

My objective is to create a JSON object out of this string.

With JSON.parse(jsonString), I get the following result:

{ 
    id: 'eb2552e1673f4ad6bb70923e5920bbab',
    Int32: 2147483647,
    Int64: 9223372036854776000,
    Int64Array: [ 9223372036854776000, 9223372036854776000 ],
    Object:
    { 
        Int321: 2147483647,
        Int641: 9223372036854776000,
        Int64Array1: [ 9223372036854776000, 9223372036854776000 ] 
    } 
}

As you can see from above 9223372036854775807 and 9223372036854775806 change to 9223372036854776000 and that is really not acceptable.

Then I read about BigInt and using the reviver parameter in JSON.parse and changed my code to something like:

const parsedJSON2 = JSON.parse(jsonString, (key, value) => {
  if (typeof value === 'number') {
    return BigInt(value).toString();
  } else {
    return value;
  }
});

And this time the output is:

{ 
    id: 'eb2552e1673f4ad6bb70923e5920bbab',
    Int32: '2147483647',
    Int64: '9223372036854775808',
    Int64Array: [ '9223372036854775808', '9223372036854775808' ],
    Object:
    { 
        Int321: '2147483647',
        Int641: '9223372036854775808',
        Int64Array1: [ '9223372036854775808', '9223372036854775808' ] 
    } 
}

This is somewhat better than the previous one, however here 9223372036854775807 and 9223372036854775806 change to 9223372036854775808 so it is still not accurate.

I am wondering if there's a better way to parse JSON containing really large numbers. I am completely fine converting these numbers into strings as long as I can have 100% fidelity in conversion.

Any help regarding this will be highly appreciated.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • 1
    You need send a string from server side. By the time `typeof value === 'number'` runs, it would be parsed to number and lost it's precision – adiga Jun 06 '20 at 11:31
  • @adiga...Thank you for your comment. Much appreciated. Unfortunately the server is not in my control :(. I am consuming an API in my application and this is the response I am getting from the API. – Gaurav Mantri Jun 06 '20 at 11:35
  • 1
    You'd have to use a library ([like this](https://stackoverflow.com/a/18755261/3082296)) OR use some regex to add quotes around big numbers before parsing. – adiga Jun 06 '20 at 11:42
  • 1
    Because Javascript treat all numbers as `double`. The MAX_Value without losing precision is `9007199254740991`.the only way is to make double parse by iterating the object and convert numbers to strings then parse them to JSON. – Menai Ala Eddine - Aladdin Jun 06 '20 at 11:43
  • @adiga....using `json-bigint` package solved it! Thank you so much for pointing me in the right direction. Would you mind closing this question as duplicate of the one you linked in your comment? Thanks!!! – Gaurav Mantri Jun 06 '20 at 12:07

0 Answers0