The CreatorCon Call for Content is officially open! Get started here.

Accessing Json objects keys with having space within the name.

abhinavjoshi
Mega Contributor

So I have this rest call, which is returning this sample data.

var test =

                      {

                      "id": "testtset",

                      "name": "sf-rg",

                      "tags": {

                              "Tag Set": "005",

                              "User Name": "Bond"

                      },

                      "properties": {

                              "provisioningState": "Succeeded"

                      }

              },

              {

                      "id": "365tset",

                      "name": "Test365",

                      "location": "us",

                      "properties": {

                              "provisioningState": "Succeeded"

                      }

              }

console.log(test.tags["User Name"]);

If I run this it will give me an error.

My json object contain values for user name but not for all ID's.

Let's say if I just have one json object console.log(test.tags["User Name"]); this will work fine, But not for multiple data objects.

Does any one know how to resolve this issue?

7 REPLIES 7

Thank you so much Chuck, this suggestion helped.


alikuttyka


The final script is like this for the json data.



      for (var i = 0; i < test.length; i++) {


        if (test[i].tags == null) {


        console.log("Null Condition");


        }


        else {


console.log(test[i].tags["User Name"]);        


        }


}


Alikutty A
Tera Sage

Hi Abhinav,



Please try this code, This should log Bond



var test =     [{


      "id": "testtset",


      "name": "sf-rg",


      "tags": {


      "Tag Set": "005",


      "User Name": "Bond"


      },


      "properties": {


      "provisioningState": "Succeeded"


      }


      }, {


      "id": "365tset",


      "name": "Test365",


      "location": "us",


      "properties": {


      "provisioningState": "Succeeded"


      }


      }]


console.log(test[0].tags["User Name"]);



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


You have passed test[0] but we can not know that value each time, is there a generic way to do it?