0

I have a object contains NaN, I would like to keep its type, after stringify.

const api = { x: 5, y: NaN }

console.log(JSON.stringify( api ));


// output  '{"x":5,"y":null}' 

//but I expect  '{"x":5,"y":NaN}'  

The reason I want to make it appear as NaN is that this json is preparing for python to process. And in the python code, they can only handle NaN, but not null. This I can not contronl

Ae Leung
  • 300
  • 1
  • 12
  • 1
    This does not work because `NaN` is not an valid JSON data type https://www.w3schools.com/js/js_json_datatypes.asp – bill.gates Sep 06 '22 at 10:16
  • 2
    `NaN` isn't a valid value in a JSON string - [see](https://www.json.org/json-en.html): "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array" – Nick Parsons Sep 06 '22 at 10:16
  • You need to talk to the people who wrote poor Python. https://stackoverflow.com/questions/6601812/sending-nan-in-json – mplungjan Sep 06 '22 at 10:45
  • @NickParsons Thanks, but I have one question. In Json, we said that {x: NaN} is invalid. However, when we do const x = {x: NaN} in console, it is doable. May I know how to interpret this ? – Ae Leung Sep 06 '22 at 11:11
  • @bill.gates Hi bill gate, May I ask, In Json, we said that {x: NaN} is invalid. However, when we do const x = {x: NaN} in console, it is doable. May I know how to interpret this ? – Ae Leung Sep 06 '22 at 11:12
  • 1
    @AeLeung A JSON string and an object literal are two different things. A JSON string is a way of representing data in a string format & needs to follow certain rules (eg: double quotes around keys, can only have specific values). Object literals on the other, such as `const x = {x: NaN}` look similar to the format of a JSON string, but are different. The values can hold any JavaScript values and the keys don't need to be in quotes when you create it (they can also use Symbols for keys). – Nick Parsons Sep 06 '22 at 11:15
  • 1
    it's very clear thanks ! – Ae Leung Sep 06 '22 at 11:16

1 Answers1

4

Since JSON does not support NaN i suggets writing your own custom replacer function.

const api = { x: 5, y: NaN }

function replacer(key, value) {
  return Number.isNaN(value) ? "NaN" : value
}
console.log(JSON.stringify(api, replacer));

And appropriate parser

const jsonData = '{"x":5,"y":"NaN"}';

function parser(key, val) {
    return val === "NaN" ? NaN : val;
}

console.log(JSON.parse(jsonData, parser));

Javascript object and JSON are not the same. JSON is just an open standard file format and data interchange format. It's format is similar to that of javascript objects but not the same.

JSON cannot contain long ints, NaNs, functions, symbols, circular references etc.

Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
  • May I know how to make it as literally NaN? coz when python real it as string, but not NaN – Ae Leung Sep 06 '22 at 10:28
  • 2
    You'll have to parse it Python-side as `NaN` if you specifically want to save that value. You'd read a `"NaN"` and somehow parse it into `NaN` manually. To simplify, I'd suggest to handle this differently though, maybe as a `null` value. – venir Sep 06 '22 at 11:00
  • Thanks, but I have one question. In Json, we said that {x: NaN} is invalid. However, when we do const x = {x: NaN} in console, it is doable. May I know how to interpret this ? – Ae Leung Sep 06 '22 at 11:11
  • @AeLeung could you provide a code sample of how you process the data in your python code? – Krzysztof Krzeszewski Sep 09 '22 at 09:54