-2

Can anyone explain me this strange behavior in JSON.parse() function in Javascript?

When calling it with string, it should raise an error.

e.g

JSON.parse("5ffc58ed1662010012d45b30"); 

result with:

VM230:1 Uncaught SyntaxError: Unexpected token f in JSON at position 1
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

... BUT!!

When I call it with this specific value:

JSON.parse("60000528880e130012727947");

It return Infinity??? Why??? How this possible? What so special in this string? Is this because this string is entirely consisting numbers and e in the middle? so JSON.parse thinks it's a kind of float?

enter image description here

ET-CS
  • 6,334
  • 5
  • 43
  • 73
  • Most probably because 10e6 kind of string is a valid numeric value as it represents 10 raised to the power 6. But d or f does not have a valid significance here. So JSON parse considers the second case as a valid Number in js and hence this behaviour – Shreyash bhatt Jan 17 '21 at 20:01
  • Yeah. JSON supports e-notation numbers (e.g. `1e3 == 1000`) and the 2nd input matches the [general pattern for them](https://www.json.org/json-en.html). Though, the exponent, 130012727947, is too large to fit into a 64-bit floating pointing, so it defaults to Infinity. – Jonathan Lonowski Jan 17 '21 at 20:02
  • In your case the number is too large to be representated as a numeric value and hence to avoid overflow error it is converted to INFINITY which is the max value in js – Shreyash bhatt Jan 17 '21 at 20:02
  • `"5ffc58ed1662010012d45b30"` this is not valid JSON. It's not valid type https://www.json.org/json-en.html – evolutionxbox Jan 17 '21 at 20:05
  • There isn't any string in the JSONs presented in the question. – axiac Jan 17 '21 at 20:07
  • For anyone who wonder why I try to parse a `string` and not `json`... I'm debugging a bug in `swagger-tools` library, which result in this behavior. – ET-CS Jan 17 '21 at 20:14

2 Answers2

1

JSON is a text representation of same data, usually a structure like an array or an object but a primitive value like a string or a number can also be represented as JSON without problems.

Being a text, in the source code it is represented as a JavaScript string.
The line:

JSON.parse("60000528880e130012727947");

can be as well JSON.parse(x), where x is a variable that contains the JSON.

The JSON in the example above is exactly this: 60000528880e130012727947 (there are no quotes around it, the quotes are the way a text is represented in the JavaScript source code). It is the text representation of a real number, 60,000,528,880 * 10^130,012,727,947, to be more precise.

JavaScript uses double-precision 64-bit binary format IEEE 754 to represent the number. The largest value a Number can hold is about 1.8×10^308 which is very much for most practical purposes. However, it is a small value compared to the value you have represented as JSON.
Anyway, no matter how large it is, since the value stored as JSON is larger than the greatest value that can be represented using the 64-bit double-precision format, Infinity is used instead.

Regarding the other example, 5ffc58ed1662010012d45b30 is not a valid representation of a number, therefore the JSON parser throws an error when it reaches the first f character at index 1.

All in all, JSON.parse() works fine, your input is not always valid JSON.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

Basically it is considering the second string as a big number

hassanqshi
  • 353
  • 2
  • 9