Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.Stringfy method's replacer argument not working on nested object

I have an object, I want to send this object's simplified version to the server.

{
 "fullName": "Don Corleone",
 "actor": {
  "actorId": 2,
  "name": "Marlon",
  "surname": "Brando",
  "description": "Marlon Brando is widely considered the greatest movie actor of all time... ",
  "heroList": [],
  "photo": "C:\\projects\\files\\actor\\1532955376934.png"
 },
 "heroProfilePhoto": "data:image/png;base64,/9j/...
 "production": {
  "title": "The Godfather",
  "imdbRate": 9.2,
  "genre": "8",
  "releaseDate": "1972-03-23T21:00:00.000Z",
  "director": "Francis Ford Coppola",
  "writer": "Mari Puzo",
  "detail": "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son."
 }
}"

I have two question::

1) Is it possible to extract something like this with replacer parameter of JSON.stringify() ?

 {
  "fullName": "Don Corleone",
  "actor": {
   "actorId": 2
   }
}"

2) At least can I extract something like this with replacer parameter of JSON.stringify()?

{
 "fullName": "Don Corleone",
 "actor": {
  "actorId": 2,
  "name": "Marlon",
  "surname": "Brando",
  "description": "Marlon Brando is widely considered the greatest movie actor of all time... ",
  "heroList": [],
  "photo": "C:\\projects\\files\\actor\\1532955376934.png"
 },
}"

When I am using like this it's okay:

JSON.stringify(hero, ['fullName']) Result -> "{"fullName":"Don Corleone"}"

But this :

JSON.stringify(hero, ['fullName', 'actor']) Result -> "{"fullName":"Don Corleone","actor":{}}"

Why actor property is empty?

like image 580
Muhammed Ozdogan Avatar asked Aug 03 '18 12:08

Muhammed Ozdogan


People also ask

Does JSON Stringify working on nested object?

If your deeply-nested object or array only includes certain primitive values (strings, numbers, boolean, and null), then you can use JSON. parse() & JSON. stringify() to deep copy without issues.

What causes JSON Stringify failure?

JSON. stringify() throws an error when it detects a cyclical object. In other words, if an object obj has a property whose value is obj , JSON. stringify() will throw an error.

What is replacer in JSON Stringify?

Parameters. value. The value to convert to a JSON string. replacer Optional. A function that alters the behavior of the stringification process, or an array of strings or numbers naming properties of value that should be included in the output.

What is difference between JSON parse and JSON Stringify?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.


1 Answers

JSON.stringify requires you pass in all of the data you would like returned. 'actor' on its own is not enough.

You want:

JSON.stringify(hero, ['fullName', 'actor', 'actorId'])

EDIT

So I've done some testing, I was curious what would happen if actorId also existed in the parent object, and the results are in.

Both actorId fields are returned by JSON.stringify() in the case where actorId exists inside of the actor object, and inside the parent object. If you would like this not to be the case, you will have to create a more complex function for your needs and pass it to JSON.stringify() as documented here

Here's some examples:

var json = {
    key1: "Value for key1 in parent",
    key2: {
        key3: "Value for key3 in child",
        key4: "Value for key4 in child"
    },
    key4: "Value for key4 in parent"
}

var out1 = JSON.stringify(json, ['key1', 'key3'])
/*
out1 = {
    key1: "Value for key1 in parent"
} // No key3!
*/


var out2 = JSON.stringify(json, ['key1', 'key2', 'key3'])
/*
out2 = {
    key1: "Value for key1 in parent",
    key2: {
        key3: "Value for key3 in child"
    }
}
*/

var out3 = JSON.stringify(json, ['key1', 'key2', 'key3', 'key4'])
/*
out3 = {
    key1: "Value for key1 in parent",
    key2: {
        key3: "Value for key3 in child",
        key4: "Value for key4 in child" // Both key4 returned
    },
    key4: "Value for key4 in parent" // Both key4 returned
}
*/
like image 76
Woohoojin Avatar answered Sep 21 '22 04:09

Woohoojin