Parse JSON Array

bdsibert
Tera Contributor

Is there a way to parse JSON in the following format:

 
{
  "data": [
    "pmgsbx1",
    "pmgsbx2",
    "pmgsbx3",
    "pmgsbx4",
    "pmgsbx5"
  ]
}​
 
I know if the JSON were like the below I could have code like the following to add options to a form:
    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?

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage
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]);

    }
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

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!

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!

vkachineni
Kilo Sage
Kilo Sage
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]);

    }
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

bdsibert
Tera Contributor

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!