Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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
Mega 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

Chuck Tomasi
Tera Patron

FWIW, I did something almost exactly like this on Episode 33 of TechNow.

TechNow Ep 33 | GlideAjax - Developer Community - ServiceNow Community

bdsibert
Tera Contributor
Ahh, thank you very much Chuck! I’ve made it a habit to watch the TechNow episodes and your live streams but apparently didn’t go back that far. Thank you for all of the content you put out—it’s quite amazing! Cheers!