Links
- JSON
- stringify(value[, replacer [, space]])
replacerparametertoJSON()- If an object being stringified has a
property named
toJSONof type Function, then the return value fromtoJSON()is serialized instead of the object itself.
- If an object being stringified has a
property named
- Note:
toJSONis used before the object is passed to thereplacer. This can be an issue forDateobjects. See this StackOverflow thread.
- parse(text[, reviver])
- stringify(value[, replacer [, space]])
On JSON.stringify
Ref: stringify(value[, replacer [, space]])
- Properties are stringified in arbitrary order.
- Non-primitive objects with primitive equivalents are serialized as primitives. (e.g. Boolean, Number and String)
- If
undefined, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).
Example:
JSON.stringify({ foo: function() {}, arr: [function bar() {}, undefined, new String("strobj")] });
Output
{"arr":[null,null,"strobj"]}
replacer parameter to JSON.stringify
Refer MDN: replacer parameter
The replacer parameter is either a function OR an array.
As a function, it has the properties:
- Parameters
key: object in which the "key" was found.value: value of that key in the object.
this: set to the object from which "key" and "value" have been pulled.- First call: In the very first call,
keyis the empty string andthisis the original object passed toJSON.stringify. - Return value
string: this string is used as the property's value instead of the originalvalue.numberorbool: the string corresponding to the number, or "true"/"false" for bool, is used as the property's value.undefinedORFunction: this key/value is skipped.- something else: recursively stringify this object instead of the original value (using this same replacer.)
- Note: You can't use the replacer function to remove values from an array.
nullis used if you returnundefinedorFunctionfrom the replacer.
reviver parameter to JSON.parse
Refer MDN: reviver parameter
The parsed JS object and all it's properties are recursively
(starting from the most nested) transformed using the
reviver to reconstruct the final object.
- Parameters
key: property namevalue: property value
this: object containing thatkeyandvalue.- Last call: In the final call,
keyis set to the empty string. The result of this call is the final result ofJSON.parseso you must handle this case. - Return value: If
undefined, then that property is skipped. Else, the result is used as the value instead.
Sample reviver snippet
function _prefixAllKeys(obj, prefix) { if (obj instanceof Object && Object.getPrototypeOf(obj) === Object.prototype) { var result = {}; for (var k of Object.keys(obj)) { result[prefix + k] = obj[k]; } return result; } else { return obj; } } function jsonReviver(key, obj) { return _prefixAllKeys(obj, "ckck_"); } var o1 = { a: [1, 2], b: { c: 1} }; var json = JSON.stringify(o1); // logs: { ckck_a: [ 1, 2 ], ckck_b: { ckck_c: 1 } } console.log(JSON.parse(json, jsonReviver));