In JavaScript, is there a way of parsing JSON that turns numbers into strings?

As JavaScript uses floating point maths for all numbers, which does not preserve precision in all cases, I want to parse a JSON object containing numbers but generate strings in the returned JavaScript object rather than number objects. Is there a way to do this using standard or third party libraries?

  • Single precision float emulation in Javascript (float32)
  • Why does multiplying and dividing by N “fix” floating point representation?
  • Why eval(“475957E-8905”) == “475957E-8905” is true?
  • Implementing an accurate cbrt() function without extra precision
  • Formatting floating-point numbers without loss of precision in AngularJS
  • 1.265 * 10000 = 126499.99999999999?
  • Input number validation - Restrict to enter only numbers or digits, int & float both
  • Reinterpret cast floating point number to integer
  • How to work around the decimal problem in JavaScript?
  • regular expression for finding decimal/float numbers?
  • Using bitwise OR 0 to floor a number
  • Convert a number into a 16-bit float (stored as bytes) and back?
  • 2 Solutions collect form web for “In JavaScript, is there a way of parsing JSON that turns numbers into strings?”

    You can do something like this. This may not be a perfect solution (especially the reg expression), but hope it will help you to solve this.

    var json = '{"num1":123.89075576575775676575, "num2":89.5564764646476444844, "num3":56.4353454354353535435435435}';
    
    json = json.replace(/\d+.\d+/g, function(a, b){ 
        return '\"' + a + '\"';
    });
    
    console.log($.parseJSON(json )); // I have used jQuery. Any JSON parser can be used
    

    You can try a regexp, something like:

     var numstrings = JSON.parse(json.replace(/\:([0-9.]+)(,?)/g,':\"$1\"$2'));
    

    http://jsfiddle.net/rmwcZ/