12

I'm trying to tackle the issue with JSON.parse() rounding big numbers. I know why this is happening but am looking for away around it.

I was thinking a regex which could parse the JSON text and turn all the big ints into strings.

I'm using JavaScript in a Windows 8 app and its got to be dealt with client side.

Got me stumped so far.

The main issue is I have to do this after I receive the response from an XMLHTTPRequest and cant change the original format of the JSON

e.g

{ "data" : {
    "username":"Brad", "userID":941022167561310208, "location":"London"
    }
}
BradStevenson
  • 1,974
  • 7
  • 26
  • 40

3 Answers3

11

Disclaimer:

This is an old answer, and the question is even older. You shouldn't ever be parsing JSON with regular expressions - it's too brittle of a solution, and is liable to break at random. The right way to do this is to parse the entire JSON string, then use code to convert string values to numbers, e.g. parseInt(jsonData.data.userID).

An even better way to do it is to fix it on your back-end so your client doesn't have to do this.


Here's the regex to wrap all integers with quotes. If you wanted to do this only for long strings, change + to {9,} (or any int).

var json = ('{"data":{"username":"Brad","userID":941022167561310208,"location":"London","test":"3908349804","test2":"This already exists in 034093049034 quotes"},"list":[3409823408,3409823408,"A list 234908234"]}');
json = json.replace(/([\[:])?(\d+)([,\}\]])/g, "$1\"$2\"$3");
json = JSON.parse(json);

See example: http://jsfiddle.net/remus/6YMYT/

Edit Updated to accommodate lists as well.

Here's the regex in action: http://regex101.com/r/qJ3mD2

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Perfect, is there any way you could do this which wouldn't effect numbers already wrapped in quotes? – BradStevenson Dec 05 '13 at 19:39
  • Updated again, now it handles lists – brandonscript Dec 05 '13 at 20:07
  • 2
    Solved my problem 99%, I was also working with decimals, so I had to just add to the digit selector part of the regex. The new one is:`json = json.replace(/([\[:])?([\d\.]+)([,\}\]])/g, "$1\"$2\"$3");` – Josh G Feb 02 '18 at 21:14
  • Hey @JoshG see edit. While this technically works, it's really brittle, and I can't in good conscience recommend it. – brandonscript Feb 02 '18 at 21:23
  • @brandonscript I saw, I just wanted to ensure that this was the issue we were facing, and then I figured I'd add what I found, while this doesn't come up much we'll most likely be changing the backend to just not pass raw numbers, thanks for following up with the wisdom and maintaining your answers! – Josh G Feb 02 '18 at 21:57
5

I know this is a very old question, but this is the solution you can use if you are using npm. Javascript can handle bigInt of value 9007199254740991 docs. You can use this npm package to parse JSON's containing large numbers.

Install your package

npm i json-bigint

Then use it like this.

var JSONbig = require('json-bigint');
let result = JSONbig.parse(<your-json>);
Touqeer
  • 150
  • 1
  • 10
  • 2
    And if you are already using BigNumber.js you can use this npm package json-bignumber: https://www.npmjs.com/package/json-bignumber – Nouran S. Ahmad Apr 23 '19 at 06:21
3

If you need to use it as a string then just save it as a string, with "":

{ "data" : {
    "username":"Brad", "userID":"941022167561310208", "location":"London"
    }
}

You also have a misstake username => "username".

You can also parse it to integer with parseInt() later

For updated question: Then you need to surround your number with "" with regexp:

s='{"data":{"username":"Brad","userID":9410221675613105435234324235235235235235523208,"location":"London"}}';
s = s.replace(new RegExp('"userID":([0-9]+),',"g"),'"userID":"$1",')

example: http://jsfiddle.net/E4txx/

mdolbin
  • 936
  • 4
  • 8