28

I have JavaScript Object say:

var a = {b: Infinity, c: 10};

When I do

var b = JSON.stringify(a);

it returns the following

b = "{"b":null, "c":10}";

How is the JSON.stringify converts the object to strings?

I tried MDN Solution.

function censor(key, value) {
  if (value == Infinity) {
    return "Infinity";
  }
  return value;
}
var b = JSON.stringify(a, censor);

But in this case I have to return the string "Infinity" not Infinity. If I return Infinity it again converts Infinity to null.

How do I solve this problem.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • Maybe for some use cases it's just fine to use another low or high number: `Number.MIN_SAFE_INTEGER` or `Number.MAX_SAFE_INTEGER` – Stefan Rein Jan 29 '20 at 22:07

4 Answers4

18

Like the other answers stated, Infinity is not part of the values JSON can store as value.

You can reverse the censor method on parsing the JSON:

var c = JSON.parse(b, function (key, value) {
    return value === "Infinity" ? Infinity : value;
});
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • This I can always do, but it would be nice to get the Infinity not "Infinity", as I don't need to perform this additional check on my hundreds of objects having hundreds of properties. – me_digvijay May 20 '13 at 07:42
  • 3
    As I wrote in my answer, a JSON file containing `Infinity` (without quotes) as a value won't be valid JSON. You shouldn't want to write non-standard-compliant JSON files. – JohnB May 20 '13 at 07:43
  • @DigvijayYadav, I fear you'll have to live with the nature of the beast. – KooiInc May 20 '13 at 07:56
  • Also see [`reviver` parameter in docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter) – djvg Aug 28 '23 at 12:31
8

JSON doesn't have Infinity or NaN, see this question:

JSON left out Infinity and NaN; JSON status in ECMAScript?

Hence { b: Infinity, c: 10 } isn't valid JSON. If you need to encode infinity in JSON, you probably have to resort to objects:

{
    "b": { "is_infinity": true, "value": null },
    "c": { "is_infinity": false, "value": 10 }
}

This structure is generated by, given your above example does what you say it does,

function censor(key, value) {
  if (value == Infinity) {
    return JSON.stringify ( { is_infinity: true, value: null } );
  } else {
    return JSON.stringify ( { is_infinity: false, value: value } );
  }
}
var b = JSON.stringify(a, censor);
Community
  • 1
  • 1
JohnB
  • 13,315
  • 4
  • 38
  • 65
5

JSON doesn't support infinity/Nan.

Please refer the below links.

http://www.markhansen.co.nz/inspecting-with-json-stringify/

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

JSON left out Infinity and NaN; JSON status in ECMAScript?

Thanks,

Siva

Community
  • 1
  • 1
SivaRajini
  • 7,225
  • 21
  • 81
  • 128
0

I encountered this lack of support when sending an object from an expressJS server using res.json(). When Infinity value was present the client-side was receiving the value null in its place.

I'm just posting what I did for my particular scenario, as it may be useful to someone coming here for a similar scenario.

In my particular instance the possible options for this specific value where either a number or Infinity.

So I ended up converting that value to a string with toString() (so a number, for example, 250 would become a string '250' and Infinity would become 'Infinity').

Then on the client-side I would convert the value to a number using Number(). That would revert Infinity to the number type Infinity.

So here is what it looks like:

The Object

myObj = {
  numberValue: Infinity, // or some other number
  otherKey: 'otherValue'
}

On server

myObj.numberValue = myObj.numberValue.toString();
res.json(myObj);

On client

fetch('/some-path', options)
  .then(res => res.json())
  .then(data => {
    data.numberValue = Number(data.numberValue);
    // do stuff with the data
  })
H K
  • 1,062
  • 8
  • 10