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?
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.
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.
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.
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.
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
}
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With