'How to adapt JSON.Parser for NaN in Javascript?
I'm trying to adapt JSON.Parse() for NaN.
console.log(JSON.parse('{"n": 1}'));
console.log(JSON.parse('{"n": NaN}'));
1st one is {n:1}.
2nd one has an error which says Unexpected token N in JSON.
And I want to change NaN to 0 like {n: 0}.
I found a resource which create JSON Parser from scratch. https://lihautan.com/json-parser-with-javascript/#implementing-the-parser
It's good he separates keys and values so that I can check only values if there is NaN and fix it. But the problem is I have no idea where I can put the NaNParser() and the process. Because the parameter looks coming 1 by 1 character.
If you could give me some advice, it really helps me. https://codesandbox.io/s/json-parser-with-error-handling-hjwxk?from-embed
Solution 1:[1]
Context
Note that JSON.stringify({"n": NaN}) outputs: '{"n":null}'
That means:
NaNis not a valid serialised value.- by default it is converted to
nullnot0(because it's not a number)
Solution
Perhaps the fastest approach is to simply replace all NaN values before parsing it.
const input = '{"o": 1, "n": NaN, "m": "test"}'
const output = input.replace(/(:\s*)NaN(\s*[,}])/, '$1null$2')
console.log(output) // prints: '{"o": 1, "n": null, "m": "test"}'
Alternatively you could use sed in the terminal:
sed -e "s/pattern/replacement/g" <input.txt >output.txt
Solution 2:[2]
"Hacky" solution:
var json = '{"n": NaN}';
var object = JSON.parse(json.replace("NaN", "\"NaN\""));
for(var key in object)
{
if(object[key] == "NaN")
{
object[key] = NaN;
}
}
console.log(object);
This replaces all NaN values in the JSON with the string "NaN", then runs that through JSON.parse, and then re-replaces all "NaN" string in the object with actual NaN.
Solution 3:[3]
If you need to produce JSON with zero values in places where NaN were in the source, you can use quite simple serializer ("replacer") during native JSON.stringify
console.log(
JSON.stringify(
// source data:
{
a: 1,
b: NaN
},
// "replacer":
function(key, value) {
if (Number.isNaN(value)) {
return 0
}
return value
},
// optional, indent character for formatting:
"\t"
)
)
To preserve NaN values, you'd have to provide custom replacer that will produce some unique structure representing it in syntactically valid JSON string, and use adequate "reviver" while doing JSON.parse to retrieve it back:
var original_data = {
a: 1,
NaN: NaN,
NaNaNaNa: {
Batman: true,
baNaNa: NaN,
}
}
const extra_type_value_key = "__value__";
const NaN_signal = "NaN, dude";
const NaN_representation = { [extra_type_value_key]: NaN_signal };
function replacer(key, value) {
if (Number.isNaN(value)) {
return NaN_representation;
}
return value
}
const data_JSON_string = JSON.stringify(original_data, replacer, "\t");
console.log("Data as JSON string:", data_JSON_string);
function reviver(key, value) {
if (value && value[extra_type_value_key] && value[extra_type_value_key] === NaN_signal) {
return NaN
}
return value
}
const data_POJO = JSON.parse(data_JSON_string, reviver, "\t");
console.log("Object from JSON string, should be same as original_data", data_POJO);
// unrelated
onload=()=>{with(document.querySelector('div').style)top=0,maxHeight='none'}
Also, for similar purpose, using dark practice of evil eval of JSON-like string would work:
var original_data_string = `{
a: 1,
b: NaN,
NaNaNa: {
Batman: NaN,
baNaNa: 3,
NaN: 4
}
}`;
var data_object = eval(`(${original_data_string})`);
console.log(data_object);
For god's sake, don't do regexp replaces on JSON source strings, especially if you do not know the shape of the data!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | anton-tchekov |
| Solution 3 |
