Skip to main content
14-Alexandrite
September 29, 2016
Question

Looping through a JSON nested structure

  • September 29, 2016
  • 1 reply
  • 4204 views

Hello All,

I am having a hard time to get this use case to work: I need to iterate through a JSON nested structrure and check some fields to populate an infotable. I tried many approach but did not get it working. Here is the structure:

I need to get the value of the field "fields" and check its value, and iterate it trough all the entries inside the "histories" structure. BTW, there is only one instance of Histories inside changelog.

I tried to create an object only for the Histories doing:

var jsonHistories=jsonJira.changelog.histories;

but still cannot get it to work.

Any help is appreciated.

Cheers

Ewerton

1 reply

1-Visitor
September 29, 2016

Should be easy

// -- You need to create a DataShape with the structure you want the infotable, let's say it "MyDataShape"

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({

  infoTableName : "InfoTable",

  dataShapeName : "MyDataShpe"

});

var histories = jsonJira.changelog.histories;

var history,item;

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

    history = histories;

    for (var j=0;j<history.items.length;j++) {

        item = history.items;

        if (item.field==="whateverYouWantToCheck") {

            // -- Let's add the data to the infotable

            result.AddRow({  field: item.field, toString: item.toString, historyId: history.id });

        }

    }

}

emoreira14-AlexandriteAuthor
14-Alexandrite
September 29, 2016

Thanks Carles, I will try this approach to see if it is cleaner. For now, I seem to have achieved it by doing:

var jsonHistories=jsonJira.changelog.histories;

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

     var items=jsonHistories.items;

     for(var j=0; j<items.length;j++){

          itemField=items

       if (itemField.field=="status"){

                    // jiraStates entry object

            var newEntry = new Object();

            newEntry.date = jsonHistories.created; // STRING - isPrimaryKey = true

            newEntry.from = itemField.fromString; // STRING

            newEntry.to = itemField.toString; // STRING

             result.AddRow(newEntry);

         }

     }

}

Which is quite similar to what you are proposing