cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Looping through a JSON nested structure

emoreira
12-Amethyst

Looping through a JSON nested structure

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

2 REPLIES 2

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 });

        }

    }

}

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

Top Tags