Parsing a JSON object with Special characters in property key

I have a JSON file that looks something like this

{
    "samlp:Response": {
        "@attributes": {
            "xmlns:samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
            "ID": "_482d7b9c-3e50-47cb-aa64-4e3655352c64",
            "Version": "2.0",
            "IssueInstant": "2019-06-27T17:02:47.711Z",
            "Destination": "https://jjds-sunrise--cqsupport--c.cs102.visual.force.com/apex/ResponseReceiver",
            "InResponseTo": "Azure_9849028456"
        }
    }
}

While trying to parse this JSON using the normal way i.e.

jsonObject.samlp:[email protected]

where jsonObject is the object I created for this JSON. I am getting an error that says

"unexpected token :"

. Am I doing it wrong or is there other way to parse this?

Answers:

Answer

Take a look at Property Accessors. Consider the following:

const jsonObject = {
  "samlp:Response": {
    "@attributes": {
      "xmlns:samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
      "ID": "_482d7b9c-3e50-47cb-aa64-4e3655352c64",
      "Version": "2.0",
      "IssueInstant": "2019-06-27T17:02:47.711Z",
      "Destination": "https://jjds-sunrise--cqsupport--c.cs102.visual.force.com/apex/ResponseReceiver",
      "InResponseTo": "Azure_9849028456"
    }
  }
};


console.log(jsonObject["samlp:Response"]["@attributes"].ID);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.