Accessing Json objects keys with having space within the name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 08:36 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 09:15 AM
Thank you so much Chuck, this suggestion helped.
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"]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 08:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 08:53 AM
You have passed test[0] but we can not know that value each time, is there a generic way to do it?