2 Replies Latest reply on Sep 29, 2016 12:21 PM by ewertonm RSS
    ewertonm Creator

    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

      • Re: Looping through a JSON nested structure
        carlesc Ninja

        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[i];

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

                item = history.items[j];

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

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

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

                }

            }

        }

          • Re: Looping through a JSON nested structure
            ewertonm Creator

            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[i].items;

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

                      itemField=items[j]

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

                                // jiraStates entry object

                        var newEntry = new Object();

                        newEntry.date = jsonHistories[i].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