1

I've been trolling for hours with little success. I have the following string snippet:

{
  "S:Envelope": ,
  "S:Body": [{
        "ns2:createTokenResponse": [{
              "$": {
                "xmlns:ns2": "http://IdentitySearch.nimc/"
              },
              "return": [{
                    "loginObject": [{
                          "timestamp": ["201912220556"],
                          "token": "rO0ABXNyABB1ZGYuTG9naW5NZXNzYWdlyuR/FrgVt2ECABVaAA1hdXRoZW50aWNhdGV
                            ...

How do I access the values for timestamp or token, given the above string in Node?

Grateful for your wisdom.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
LPAudio
  • 11
  • 3
  • That is a pretty complicated JSON object. Less is more. Where did that come from? I’d like to be sure I avoid that library in the future. – zipzit Dec 22 '19 at 05:09
  • Does this answer your question? [Extracting values from deeply nested JSON structures](https://stackoverflow.com/questions/18089229/extracting-values-from-deeply-nested-json-structures) – dwjohnston Dec 22 '19 at 05:18

2 Answers2

0

// if you have a string use a = JSON.parse(a);

let a = {
  "S:Envelope": "",
  "S:Body": [{
    "ns2:createTokenResponse": [{
      "$": {
        "xmlns:ns2": "http://IdentitySearch.nimc/"
      },
      "return": [{
        "loginObject": [{
          "timestamp": ["201912220556"],
          "token": "rO0ABXNyABB1ZGYuTG9naW5NZXNzYWdlyuR/FrgVt2ECABVaAA1hdXRoZW50aWNhdGV"
        }]
      }]
    }]
  }]
};



let timestamp = a["S:Body"][0]["ns2:createTokenResponse"][0]["return"][0]["loginObject"][0]["timestamp"][0];

let token = a["S:Body"][0]["ns2:createTokenResponse"][0]["return"][0]["loginObject"][0]["token"];

console.log(timestamp);

console.log(token);
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
0

If not only this object use https://www.npmjs.com/package/jsonpath

Or just convert to object and access its properties.

Alex
  • 972
  • 1
  • 9
  • 16