I am trying to parse a JSON string with a reviver function to only include specific properties. Here is my code snippet:
const whitelist = ['prop1', 'prop2', 'result'];
const reviver = (key, value) => {
if (whitelist.includes(key)) {
return value;
} else {
return undefined;
}
};
const theMightyJsonString = '{ "result": { "prop1": "Greetings", "prop2": "Hello", "prop3": "WASSUP!!!!" } }';
console.log(JSON.parse(theMightyJsonString))
console.log(JSON.parse(theMightyJsonString, reviver))
Although I can successfully parse the JSON string using JSON.parse(theMightyJsonString)
, the result is undefined
when using
JSON.parse(theMightyJsonString, reviver)
. What could be causing this issue?