2

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);
W Kenny
  • 1,855
  • 22
  • 33
  • 2
    Possible duplicate of [Test for existence of nested JavaScript object key](https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key) – Nikhil Savaliya Jun 13 '19 at 10:30

3 Answers3

1

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);
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
1
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;
riorudo
  • 977
  • 7
  • 14
1

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

khush
  • 2,702
  • 2
  • 16
  • 35