Links
- JSON
- stringify(value[, replacer [, space]])
replacer
parametertoJSON()
- If an object being stringified has a
property named
toJSON
of type Function, then the return value fromtoJSON()
is serialized instead of the object itself.
- If an object being stringified has a
property named
- Note:
toJSON
is used before the object is passed to thereplacer
. This can be an issue forDate
objects. 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,
key
is the empty string andthis
is the original object passed toJSON.stringify
. - Return value
string
: this string is used as the property's value instead of the originalvalue
.number
orbool
: the string corresponding to the number, or "true"/"false" for bool, is used as the property's value.undefined
ORFunction
: 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.
null
is used if you returnundefined
orFunction
from 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 thatkey
andvalue
.- Last call: In the final call,
key
is set to the empty string. The result of this call is the final result ofJSON.parse
so 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));