Reputation: 339
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
Upvotes: 2
Views: 2652
Reputation: 11313
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!
Upvotes: 1
Reputation: 11313
I have no idea where I can put the NaNParser() and the process.
There is parseValue
function in that fakeParseJSON
by Tan Li Hau you are using in your code sandbox. Teaching it to recognize NaN
seems to be as easy as adding one parseKeyword
call in there:
parseKeyword("NaN", NaN)
or, since you
And I want to change NaN to 0
parseKeyword("NaN", 0)
Upvotes: 1
Reputation: 5524
Context
Note that JSON.stringify({"n": NaN})
outputs: '{"n":null}'
That means:
NaN
is not a valid serialised value.null
not 0
(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
Upvotes: 2
Reputation: 1098
"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.
Upvotes: 1