- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 02:51 PM
Is there a way to parse JSON in the following format:
{
"data": [
"pmgsbx1",
"pmgsbx2",
"pmgsbx3",
"pmgsbx4",
"pmgsbx5"
]
}
var answer = response.responseXML.documentElement.getAttribute("answer");
var objJSON = JSON.parse(answer);
for (var loop = 0; loop < objJSON.data.length; loop++)
{
g_form.addOption('test_existing_pooled_account_text', objJSON.data[loop].name, objJSON.data[loop].name);
}
{
"data": [
{
"id": 518,
"name": "PAM-ldrpcic-1",
"pam_group": "PAM-TEST",
"request_id": "RITM0483862"
},
{
"id": 519,
"name": "PAM-ldrpcic-2",
"pam_group": "PAM-TEST",
"request_id": "RITM0483862"
},
{
"id": 520,
"name": "PAM-ldrpcic-3",
"pam_group": "PAM-TEST",
"request_id": "RITM0483862"
}]
}
But I'm stuck trying to parse an array without a key value. Any thoughts?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 03:07 PM
var objJSON = JSON.parse(answer);
for (var loop = 0; loop < objJSON.data.length; loop++)
{
g_form.addOption('test_existing_pooled_account_text', objJSON.data[loop], objJSON.data[loop]);
}
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 03:06 PM
Hi,
All you need to do is use obj.data (for example).
So if you tried this in background script, just to see:
var obj = {
"data": [
"pmgsbx1",
"pmgsbx2",
"pmgsbx3",
"pmgsbx4",
"pmgsbx5"
]
}
gs.print("Data is " + obj.data);
gs.print("Length is: " + obj.data.length);
for (var i = 0; i < obj.data.length; i++) {
gs.print(obj.data[i]);
}
The array prints out.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 05:22 PM
Hi,
I'm glad you found your answer. I believe I covered how to "parse" it out and use it, etc. per your question, but it appears you may have had additional steps that you needed? Other than an example of another use case for an actual object with keys, you didn't say what you wanted to do.
Thanks
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 03:07 PM
var objJSON = JSON.parse(answer);
for (var loop = 0; loop < objJSON.data.length; loop++)
{
g_form.addOption('test_existing_pooled_account_text', objJSON.data[loop], objJSON.data[loop]);
}
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 03:34 PM
Thank you both very much! I guess I was making it a bit harder than I needed to.
Another thing to note for anybody else reading this: on the form load, my field was pre-populated from the table is I was referencing. I had to clearOptions before running my for loop as it appears the addOption function does not overwrite anything already in the field...which now makes sense as I think about it.
Thank you again!