json.parse() converts a JSON string into a JavaScript object. It accepts a string value and an optional reviver function to modify the parsed values. The document includes examples demonstrating the syntax and the use of the reviver function.
2. JSON.parse() takes a JSON string and transforms it
into a JavaScript object.
Syntax:
JSON.parse( stringValue, [reviver] );
- stringValue JSON-string to parse
- reviver function(key, value), function to
transform the value
const json = ‘{"string":"Hello
Nugget!","number":123456,
"boolean":false,"test":null}’;
let obj = JSON.parse(json);
console.log(obj);
console.log(obj.string);
//Output
{
string: “Hello Nugget!”,
number: 123456,
boolean: false,
null: null
}
Hello Nugget!
3. // Output
{
string: “Hello Nugget!”,
number: 123456,
boolean: “setting true”,
null: null
}
true
JSON.parse(jsonString, function(key, value) {
// some operation
// return value for the key
return value;
});
Using reviver function:
This function is to modify the result before
returning.
let obj = JSON.parse(json,
(key, value) => {
return key === ‘boolean’ ?
‘setting true’: true
});
console.log(obj);
console.log(obj.boolean);