javascriptjsonreactjsjson-serializationobject-serialization

JSON stringify skip nested object which has more than 3 levels


I wanted to store the object in local storage and I serialized the object using JSON.stringify. Then some of the inner attributes are missing after parse using JSON.parse.

I attached 2 images below to see the changes and appreciate it if anyone can answer a better solution for this. Thanks.

This object is just before stringified using JSON.

enter image description here

This object is stringified and parse using JSON

enter image description here

This is how i store and retreive data

enter image description here

enter image description here


Solution

  • Json.Stringify does not pass functions into the stringified JSON, i.e. functions will not be copied into the string as functions are not valid JSON objects. In your case, the difficulty is a function and as such won't be copied.

    You can include the function by using a replacer:

    JSON.stringify({
       /* your object here */
    }, function(key, val) {
            return (typeof val === 'function') ? '' + val : val;
    });