1

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);
1

3 Answers 3

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

Comments

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);

Comments

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

Comments

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.