W Kenny
W Kenny

Reputation: 2089

Angular Getting Value from Object Object

I would like to extract the value from the JSON below (resReturn.result.gamingdata.original.success)

Just wonder why I can get the value only if I do several times of stringify and parse.

Can someone tell me how to simplify my code?

JSON:

{
    "status":"Success",
    "message":"100",
    "resReturn":
    {
        "result":{
            "gamingdata":
            {
                    "headers":{},
                    "original":{"success":"Gaming Data Excel - upload success"},
                    "exception":null
            }
    }
}

}

My Code:

          let resReturnJSON = JSON.stringify(this.UploadstatusGamingDataExcel.resReturn);
          let resultobj = JSON.parse(resReturnJSON || '{}').result;
          let resultJSON = JSON.stringify(resultobj);
          let gamingdataobj = JSON.parse(resultJSON || '{}').gamingdata;
          let gamingdataJSON = JSON.stringify(gamingdataobj);
          let originalObj = JSON.parse(gamingdataJSON || '{}').original;
          let originalJSON = JSON.stringify(originalObj);
          let successObj = JSON.parse(originalJSON || '{}').success;
          console.log(successObj);

Upvotes: 1

Views: 8989

Answers (3)

khush
khush

Reputation: 2799

If you want a generalised function for json having array and objects, you can use this,

const data = {
      "status": "Success",
      "message": "100",
      "resReturn": {
        "result": {
          "gamingdata": {
            "headers": {},
            "original": {
              "success": "Gaming Data Excel - upload success"
            },
            "exception": null
          }
        }
      }
    };

    const get = (p, o) =>
      p.reduce((xs, x) =>
        (xs && xs[x]) ? xs[x] : null, o)


    console.log(get(['resReturn', 'result', 'gamingdata', 'original', 'success'], data));

I have one more simplest solution:

    let obj: any;
    try {
        if (data.resReturn.result.gamingdata.original.success) {
            obj = data.resReturn.result.gamingdata.original.success
        }      
    } catch(e) {
        obj = null
    }
    console.log(obj);

For other different ways, you can also refer this answer

Upvotes: 1

riorudo
riorudo

Reputation: 1227

const value = {
    "status": "Success",
    "message": "100",
    "resReturn":
    {
        "result": {
            "gamingdata":
            {
                "headers": {},
                "original": { "success": "Gaming Data Excel - upload success" },
                "exception": null
            }
        }
    }
}
const jsonValue = JSON.stringify(value);
const valueFromJson = JSON.parse(jsonValue);
const success = (((((valueFromJson || {}).resReturn || {}).result || {}).gamingdata || {}).original || {}).success;

Upvotes: 1

Kunal Mukherjee
Kunal Mukherjee

Reputation: 5853

Check for truthiness for every property until you hit success property and return if found or return empty string.

const data = {
  "status": "Success",
  "message": "100",
  "resReturn": {
    "result": {
      "gamingdata": {
        "headers": {},
        "original": {
          "success": "Gaming Data Excel - upload success"
        },
        "exception": null
      }
    }
  }
};

const success = (data.resReturn &&
    data.resReturn.result &&
    data.resReturn.result.gamingdata &&
    data.resReturn.result.gamingdata.original.success) ?
    data.resReturn.result.gamingdata.original.success : '';

console.log(success);

Upvotes: 1

Related Questions