alyei
alyei

Reputation: 43

Accessing nested JSON keys with multiple colons in row

I have this JSON object that I don't know how to access - the console only ever prints undefined.

I don't know how to access multiple keys with colons.

The JSON object:

 {
  'soapenv:Envelope': {
    '$': {
      'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
      'xmlns:soapenc': 'http://schemas.xmlsoap.org/soap/encoding/',
      'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema',
      'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance'
    },
    'soapenv:Header': [
      ''
    ],
    'soapenv:Body': [
      {
        'ns5:loginResponse': [
          {
            '$': {
              'xmlns:ns5': ' /* Website sending me this response */ '
            },
            'ns5:id': [
              /*Sessionkey*/
            ],
            'ns5:rc': [
              '0'
            ]
          }
        ]
      }
    ]
  }
}

What I have already tried:

console.dir(res["soapenv:Envelope"]["soapenv:Body"][0][0]['ns5:rc'])

console.dir(res["soapenv:Envelope"]["soapenv:Body"]["ns5:loginResponse"])["ns5:rc"]

... and a bunch of others I don't know anymore.

Upvotes: 2

Views: 603

Answers (3)

ProfDFrancis
ProfDFrancis

Reputation: 9431

Take it stepwise, looking at the output at each stage

Beware that there is a mixture of arrays and objects, which is where you are having difficulties. Where the output starts with the {, it is an object, so the next thing to append to your entry should be a property name. In contrast, when the output starts with [, it is an array, so the next thing to append should be a number such as [0] to obtain the first element.

res["soapenv:Envelope"]

{$: {…}, soapenv:Header: Array(1), soapenv:Body: Array(1)}

res["soapenv:Envelope"]["soapenv:Body"]

[{…}]

res["soapenv:Envelope"]["soapenv:Body"][0]

{ns5:loginResponse: Array(1)}

res["soapenv:Envelope"]["soapenv:Body"][0]["ns5:loginResponse"]

[{…}]

res["soapenv:Envelope"]["soapenv:Body"][0]["ns5:loginResponse"][0]

{$: {…}, ns5:id: Array(0), ns5:rc: Array(1)}

res["soapenv:Envelope"]["soapenv:Body"][0]["ns5:loginResponse"][0]["$"]

{xmlns:ns5: " /* Website sending me this response */ "}

res["soapenv:Envelope"]["soapenv:Body"][0]["ns5:loginResponse"][0]["$"]["xmlns:ns5"]

" /* Website sending me this response */ "

Upvotes: 2

GusRuss89
GusRuss89

Reputation: 1404

It would probably help if you ran your JSON through a prettifier to make it more readable.

The colons won't mean anything because they're part of a string.

Here's what you need to get to ns5:rc

res['soapenv:Envelope']['soapenv:Body'][0]['ns5:loginResponse'][0]['ns5:rc']

Upvotes: 0

TheCog19
TheCog19

Reputation: 1209

Take it one step at the time. The presence of the colons shouldn't affect anything, the keys are just strings. Start with seeing what res["soapenv:Envelope"] gets you. For me, in the console, i can access it just fine.

I tried res ["soapenv:Envelope"]["soapenv:Body"][0]["ns5:loginResponse"][0]["ns5:rc"] and that also worked.

If you can't access the object and its the return from a request, its very possible your router has special methods on res that you're supposed to use to access it.

Upvotes: 1

Related Questions