220

Any idea why JSON left out NaN and +/- Infinity? It puts Javascript in the strange situation where objects that would otherwise be serializable, are not, if they contain NaN or +/- infinity values.

Looks like this has been cast in stone: see RFC4627 and ECMA-262 (section 24.5.2, JSON.stringify, NOTE 4, page 683 of the ECMA-262 pdf at last edit):

Finite numbers are stringified as if by calling ToString(number). NaN and Infinity regardless of sign are represented as the String null.

4

10 Answers 10

107

Infinity and NaN aren't keywords or anything special, they are just properties on the global object (as is undefined) and as such can be changed. It's for that reason JSON doesn't include them in the spec -- in essence any true JSON string should have the same result in EcmaScript if you do eval(jsonString) or JSON.parse(jsonString).

If it were allowed then someone could inject code akin to

NaN={valueOf:function(){ do evil }};
Infinity={valueOf:function(){ do evil }};

into a forum (or whatever) and then any json usage on that site could be compromised.

4
  • 1
    I have read most of the ECMAScript spec and, of course, I knew about NaN and Infinity, but I just learned 'undefined' is actually a property of the global object and I am absolutely counfounded. Jul 17, 2022 at 14:46
  • 2
    This is nonsense. eval(jsonString) will be able to compromise anything regardless of whether NaN or Infinity are allowed. eval("alert('1')") isn't valid json but will work There's not even a way to express function()` in json. This is just a misuse of eval.
    – zkldi
    Nov 10, 2022 at 14:54
  • 5
    If you are evaluating properties using eval, any property can be used to compromise the system - it has nothing to do with NaN or Infinity.
    – Orestis P.
    Jan 19 at 13:46
  • 2
    @OrestisP. So as a heads up, when I wrote this answer, JSON.parse was not universally available. The way json was originally parsed was essentially eval(), or various scripts (json.js) that endeavored to filter/regex match a string prior to passing it to ... eval. That dictated how JSON started its existence as a "spec".
    – olliej
    Jan 22 at 10:16
64

On the original question: I agree with user "cbare" in that this is an unfortunate omission in JSON. IEEE754 defines these as three special values of a floating point number. So JSON cannot fully represent IEEE754 floating point numbers. It is in fact even worse, since JSON as defined in ECMA262 5.1 does not even define whether its numbers are based on IEEE754. Since the design flow described for the stringify() function in ECMA262 does mention the three special IEEE values, one can suspect that the intention was in fact to support IEEE754 floating point numbers.

As one other data point, unrelated to the question: XML datatypes xs:float and xs:double do state that they are based on IEEE754 floating point numbers, and they do support the representation of these three special values (See W3C XSD 1.0 Part 2, Datatypes).

7
  • 5
    I agree this is all unfortunate. But perhaps it is a good thing that JSON numbers don't specify the exact floating point format. Even IEEE754 specifies many formats -- different sizes, and a distinction between decimal and binary exponents. JSON is particularly well suited to decimal, so it would be a pity if some standard were to pin it to binary. May 3, 2014 at 4:40
  • 6
    @AdrianRatnapala +1 Indeed: JSON numbers have potentially infinite precision, so are much better than IEEE specifications, since they have no size limit, no precision limit, and no rounding effect (if the serializer can handle it). Jan 30, 2015 at 13:39
  • 6
    @ArnaudBouchez. That said, JSON should still support strings representing NaN and +-Infinity. Even if JSON should not be pinned to any IEEE format, people defining number format should at least look at the wikipedia page IEEE754 and stop a while to think. Jan 31, 2015 at 15:39
  • 2
    This is not unfortunate. See the answer by @CervEd. It is not tied to IEE754 which is a good thing (even if most programming languages use IEEE754 and thus requires additional processing in case of NaN, etc.). Mar 10, 2020 at 7:31
  • 2
    Like it or not, NaN and Infinity are perfectly valid floating point numbers supported by every processor out there. If JSON won't support them, then every user is forced to deal with this issue every time they try to encode floating point numbers in JSON. Jul 19, 2022 at 15:41
20

Could you adapt the null object pattern, and in your JSON represent such values as

"myNum" : {
   "isNaN" :false,
   "isInfinity" :true
}

Then when checking, you can check for the type

if (typeof(myObj.myNum) == 'number') {/* do this */}
else if (myObj.myNum.isNaN) {/* do that*/}
else if (myObj.myNum.isInfinity) {/* Do another thing */}

I know in Java you can override serialization methods in order to implement such a thing. Not sure where your serializing from, so I can't give details on how to implement it in the serialization methods.

9
  • 1
    hmmm... that's an answer to a workaround; I wasn't really asking for a workaround but rather for why these values were excluding. But +1 anyway.
    – Jason S
    Sep 14, 2009 at 18:46
  • 2
    @Zoidberg: undefined isn't a keyword, it's a property on the global object
    – olliej
    Sep 14, 2009 at 21:35
  • 2
    @Zoidberg: undefined is a property on the global object -- it's not a keyword, so "undefined" in this returns true in the global scope. It also means you can do undefined = 42 and if (myVar == undefined) becomes (essentially) myVar == 42. This harks back to the early days of ecmascript nee javascript where undefined didn't exist by default, so people just did var undefined in the global scope. Consequently undefined couldn't be made a keyword without breaking existing sites, and so we were doomed for all time to have undefined be a normal property.
    – olliej
    Sep 15, 2009 at 21:00
  • 3
    @olliej: I have no idea why you think undefined is a property on the global object. By default the lookup of undefined is the built-in value of undefined. If you override it with "undefined=42" then when you access undefined as a variable lookup, you get the overridden value. But try doing "zz=undefined; undefined=42; x={}; 'undefined old='+(x.a === zz)+', undefined new='+(x.a === undefined)". You can never redefine the internal values of null, undefined, NaN, or Infinity, even if you can override their symbol lookups.
    – Jason S
    Sep 17, 2009 at 15:06
  • 3
    @Jason undefined is a global property because it is specified as such. Consult 15.1.1.3 of ECMAScript-262 3rd ed.
    – kangax
    Sep 18, 2009 at 4:36
18

The strings "Infinity", "-Infinity", and "NaN" all coerce to the expected values in JS. So I'd argue the right way to represent these values in JSON is as strings.

> +"Infinity"
Infinity

> +"-Infinity"
-Infinity

> +"NaN"
NaN

It's just a shame JSON.stringify doesn't do this by default. But there is a way:

> JSON.stringify({ x: Infinity }, function (k,v) { return v === Infinity ? "Infinity" : v; })
"{"x":"Infinity"}"
6
  • 1
    0/0, etc, are not valid JSON. You have to work within the confines of the standard, and strings do the job nicely.
    – teh_senaus
    Sep 8, 2015 at 10:55
  • On the contrary, I think this is the only practical solution, but I'll do a function that return NaN if the input value is "NaN", etc. The way you do the conversion is prone to code injection. Mar 15, 2016 at 10:31
  • 4
    JSON values can't be arithmetic expressions... the goal of making the standard separate from the language literal syntax is to make JSON deeserializable without executing any of it as code. Not sure why we couldn't have NaN and Infinity added as keyword values like true and false, though.
    – Mark Reed
    Apr 18, 2016 at 10:22
  • 1
    This is work like magic.JSON.parse("{ \"value\" : -1e99999 }") easily return { value:-Infinity } in javascript. Only it just not compatible with custom number type that could be larger than that
    – Thaina Yu
    May 20, 2020 at 5:02
  • 1
    I am blown away that "N", "NaN", "NaNxxx" all parse to NaN and that "[+|-]Infinity" parse as [+-]Infinity and I didn't know it after writing JavaScript for over 25 years... Jul 25, 2022 at 13:21
7

If you have access to the serialization code you might represent Infinity as 1.0e+1024. The exponent is too large to represent in a double and when deserialized this is represented as Infinity. Works on webkit, unsure about other json parsers!

5
  • 5
    IEEE754 supports 128 bit floating point numbers so 1.0e5000 is better
    – Ton Plomp
    Oct 6, 2012 at 11:40
  • 4
    Ton: 128 bit was added later. What if they decide to add 256 bit? Then you’ll have to add more zeros, and existing code will behave differently. Infinity will always be Infinity, so why not support that? Jun 6, 2014 at 12:05
  • 1
    Clever idea! I was just about to either switch to a different format or add cumbersome workaround code to my parser. Not ideal for every every case, but in my case, where infinity serves as just an optimized edge case to a converging sequence, it's just perfect and even if larger precision would be introduce it would still be mostly correct. Thanks!
    – Or Sharir
    May 31, 2015 at 15:40
  • 5
    1, -1, and 0..... perfectly valid/parsable numbers, become those three special values when you simply add /0 to the end of them. It's easily parsable, immediately visible, and even evaluable. It's inexcusable that they haven't yet added it to the standard: {"Not A Number":0/0,"Infinity":1/0,"Negative Infinity":-1/0} <<Why not? alert(eval("\"Not A Number\"") //works alert(eval("1/0")) //also works, prints 'Infinity'. No excuse.
    – Triynko
    Sep 8, 2015 at 7:00
7

The reason is stated on page ii in Standard ECMA-404 The JSON Data Interchange Syntax, 1st Edition

JSON is agnostic about numbers. In any programming language, there can be a variety of number types of various capacities and complements, fixed or floating, binary or decimal. That can make interchange between different programming languages difficult. JSON instead offers only the representation of numbers that humans use: a sequence of digits. All programming languages know how to make sense of digit sequences even if they disagree on internal representations. That is enough to allow interchange.

The reason is not, as many have claimed, due to the representations of NaN and Infinity ECMA script. Simplicity is a core design principle of JSON.

Because it is so simple, it is not expected that the JSON grammar will ever change. This gives JSON, as a foundational notation, tremendous stability

13
  • 15
    ...only the representation of numbers that humans use... So, mathematicians, scientists, engineers and programmers that use the infinity symbol are not humans? (Snark aimed at the standard, not at CervEd.) Jul 14, 2021 at 1:22
  • 1
    That explanation really makes no sense. In practice, real world programs in most programming languages will be using floating point values that support NaN and Infinity. Humans already have ways to write numbers down and send them to each other with JSON. JSON is for computers. Based on their logic, JSON should only support strings. Jul 19, 2022 at 15:44
  • 1
    @EmileCormier, the thing is that they use the term (and the symbol) with different meanings. In mathematics, +∞ is not the same thing as ; and besides that there are aleph numbers and various other interpretations. IEEE-754's interpretation of infinities is just a one of them. And JSON (fortunately or not) decided not to stick to a specific interpretation. (BTW, support for IEEE-754's infinity and minus infinity must be accompanied with support for IEEE-754's minus zero.)
    – Sasha
    Oct 9, 2022 at 13:29
  • 1
    @Sasha I have no stats or anything to back this up, but my hunch is that the majority of use cases would want to use the same meanings as IEEE-754. Oct 9, 2022 at 21:04
  • 1
    @KellyBundy If one insists on serializing Infinity (!?) one presumably writes the application to expect either a numeric or string value for a certain field. If it's numeric and the value is represented as a string, one presumably tries to parse that in a suitable manner.
    – CervEd
    Oct 17, 2022 at 10:43
4

Potential work-around for cases like {"key":Infinity}:

JSON.parse(theString.replace(/":(Infinity|-IsNaN)/g, '":"{{$1}}"'), function(k, v) {
   if (v === '{{Infinity}}') return Infinity;
   else if (v === '{{-Infinity}}') return -Infinity;
   else if (v === '{{NaN}}') return NaN;
   return v;
   });

The general idea is to replace occurences of invalid values with a string we will recognize when parsing and replace it back with the appropriate JavaScript representation.

3
  • 1
    I don't know why this solution got a downvote because frankly, if you face a situation where your JSON string contains Infinity or IsNaN values it will fail when you try to parse it. Using this technique, you first replace occurrences of IsNaN or Infinity with something else (to isolate them from any valid string which might contain those terms), and use the JSON.parse(string,callback) to return the proper, valid JavaScript values. I'm using this in production code and never had any issue.
    – SHamel
    Jul 8, 2019 at 14:50
  • 4
    Wouldn't this mess up Infinity inside strings? For many usecases it's probably safe to assume it's not an issue, but the solution is not fully robust.
    – olejorgenb
    May 28, 2020 at 9:47
  • @olejorgenb You could wrap your data in an object that stores a string representation of the orig data type, then use this code on elements where the orig data type was not a string. True, nested objects of mixed data types take extra care!. But if you know something about the data coming in, you could use that to your advantage. In a best case scenario, if all the data are simple numbers, then this would be easy. More complex data needs more complex solution. Depends on how important it is to restore the proper data. Anyway, this code is a great start toward a robust version for complex data. Feb 5, 2022 at 18:04
4

JSON5 allows standard Javascript notation for positive and negative infinity, NaN, and numerous other things that are valid ECMAScript that were left out of JSON (trailing commas, etc.).

https://json5.org/

This makes JSON a much more useful format.

However, whether using JSON or JSON5: for security reasons, always always parse -- don't evaluate!!

1
  • 5
    Note that "JSON5" is an independent project and is not a later version of any official standard.
    – Boann
    Oct 9, 2022 at 18:49
1

The current IEEE Std 754-2008 includes definitions for two different 64-bit floating-point representations: a decimal 64-bit floating-point type and a binary 64-bit floating-point type.

After rounding the string .99999990000000006 is the same as .9999999 in the IEEE binary 64-bit representation but it is NOT the same as .9999999 in the IEEE decimal 64-bit representation. In 64-bit IEEE decimal floating-point .99999990000000006 rounds to the value .9999999000000001 which is not the same as the decimal .9999999 value.

Since JSON just treats numeric values as numeric strings of decimal digits there is no way for a system that supports both IEEE binary and decimal floating-point representations (such as IBM Power) to determine which of the two possible IEEE numeric floating-point values is intended.

1
  • 1
    What does this have to do with the question? (which is about Infinity and NaN)
    – Bryan
    Jan 3, 2019 at 12:41
-5

If like me you have no control over the serialisation code, you can deal with NaN values by replacing them with null or any other value as a bit of a hack as follows:

$.get("file.json", theCallback)
.fail(function(data) {
  theCallback(JSON.parse(data.responseText.replace(/NaN/g,'null'))); 
} );

In essence, .fail will get called when the original json parser detects an invalid token. Then a string replace is used to replace the invalid tokens. In my case it is an exception for the serialiser to return NaN values so this method is the best approach. If results normally contain invalid token you would be better off not to use $.get but instead to manually retrieve the JSON result and always run the string replacement.

2
  • 22
    Clever, but not entirely foolproof. Try it with { "tune": "NaNaNaNaNaNaNaNa BATMAN", "score": NaN }
    – JJJ
    Apr 4, 2013 at 4:36
  • 1
    and you must be using jQuery. I don't have $.get().
    – Jason S
    Apr 4, 2013 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.