6

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?

2
  • 1
    You have not specified any other properties than the actor & fullname, and the actor is indeed been sent. If you want more inside the actor, you need to select that too.. eg. -. JSON.stringify(hero, ['fullName', 'actor', 'name']) Commented Aug 3, 2018 at 12:22
  • O I have tried this: JSON.stringify(hero, ['fullName', 'actorId']) and than it returned -> "{"fullName":"Don Corleone"}" But this: JSON.stringify(hero, ['fullName', 'actor', 'actorId']) returned the what I want -> "{"fullName":"Don Corleone","actor":{"actorId":2}}" Thank you. So before 'actorId I have to add 'actor' property. Commented Aug 3, 2018 at 12:30

2 Answers 2

6

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
}
*/
Sign up to request clarification or add additional context in comments.

Comments

0

Easily we can get by using Spread in object literals

Try to read the Object like

const obj = {yourObj.fullname, yourObj.actor} const yourAnswer = JSON.stringify(obj)

you can refer the link Spread_syntax

since JSON.stringify is very slow for large objects, try to use above method

2 Comments

What I look is the JSON.stringify() because of the simplicity of 'stringify'. Thank you 'Ananth' .
since JSON.stringify is very slow for large objects, should we prefer Spread in object literals

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.