0

I have been searching for hours with no luck on this although I am sure it is something simple and obvious. I have the below JSON object which I receive from an API call as a string and then use JSON.parse to make into an object. I can use standard notation like obj.Header.Statuscode or obj.Body to access the data. However, no matter what I try I cannot access the LIPIdList object. I thought it should be accessible via obj.Body.LIPIdList but I get an undefined error. Please can someone explain to me what I am missing here? Thanks so much!

{ "CommuniqueType": "ReadResponse",
"Header": {
    "MessageBodyType": "OneLIPIdListDefinition",
    "StatusCode": "200 OK",
    "Url": "/server/2/id" },
"Body": {
    "LIPIdList": {
        "Devices": [
     { 
        "Name": "Smart Bridge",
        "ID": 1,
        "Buttons": [
        {
          "Name": "Test",
          "Number": 1
        },

      ]
    },
    {
      "Name": "Pico Test",
      "ID": 3,
      "Buttons": [
        {
          "Number": 2
        },
        {
          "Number": 3
        },
        {
          "Number": 4
        },
        {
          "Number": 5
        },
        {
          "Number": 6
        }
      ]
    }
  ],
  "Zones": [
    {
      "Name": "Office",
      "ID": 2
    }
  ]
}}}
  • 1
    What you've written is definitely how it would be accessed. – Pointy May 10 '17 at 04:30
  • `obj.Body.LIPIdList` *should* work, and does for me (in console, `obj = ; obj.Body.LIPIdList` shows the expected result.) – Amadan May 10 '17 at 04:30
  • Show us your JS code snippet to understand it more. – Manwal May 10 '17 at 04:31
  • When you get "an undefined error" then that means you are trying to access a property that doesn't exist. This could happen if you either haven't parsed the JSON or the structure of the object you are actually having is different. – Felix Kling May 10 '17 at 04:35
  • Thank you all for your quick response. You all verifying that I was using the correct notation made me think that there must be something else wrong. I really don't know how to explain it, but when I added "if (result.toString().indexOf('LIPIdList') !== -1) {" and the did the JSON.parse it worked correctly. I don't really understand it because when I would call obj.Body it would show the correct piece of the object including the LIPIdList. I am doing this in node so maybe the large size of the JSON object (I cut out a large piece of it) wasn't done being parsed before it tried to access it? – Nate Schwartz May 10 '17 at 04:42

0 Answers0