Working with API in javascript that uses identifiers above JS max integer

I am creating a Node backend that retrieves data from the Vine API and returns this to my interface. Identifiers for messages and users are of type Number, and they often exceed the maximum integer that javascript can support in accordance with this question .

The example identifier from the API is 934416748524998656 . When I use JSON.parse at the front end, it replaces these last 2 digits with 0, because it cannot read the number which is high. This causes problems when I try to talk to the API with specific identifiers, since now it does not recognize the identifier .

Is there anything other than creating a new service in another language that I can do to work with these identifiers? I tried using toString() to interpret numbers as strings instead, but this just creates a string of an already malformed number.

Thanks for any help

+5
source share
1 answer

change the number to a string before parsing:

 var responseData = JSON.parse( strJson.replace(/\: (\d{17,}),/g,': "$1",') ); 

Please note that this will lead to a false change in any string data that contains a large number that looks like a json value, but I would call it a worthy compromise compared to deep investment alternatives. however, this is not just a large number that hits, but (":" + theNumber + ","); the only time I can imagine what is happening is someone “repeating the tweet” of the JS invalid API response. use a parser if you care about being perfect.

+5
source

Source: https://habr.com/ru/post/1200130/


All Articles