cancel
Showing results for 
Search instead for 
Did you mean: 

Iterating through JSONModel with multiple nested arrays

Ron_Hagan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

I have  UI5 application that executes a GET request and returns the data to the UI as a JSONModel.  I'm trying to iterate through the results to get to the data I want but am having challenges.

Below is a sample of the data with just 2 results.  I'm trying to get to the oData section and I'm obviously not doing it right.

Interesting that in the Dev Tools of Chrome, it appears a little differently, showing the indexes of the array rows in oData.  I put a screen shot below the JSONModel data.

Any ideas and/or suggestions anyone has would be greatly appreciated.

Thanks in advance!

Ron

***********************************************************************

JSONModel data:

{
"pSequentialImportCompleted": {},
"mEventRegistry": {},
"mMessages": {},
"id": "id-1713560453764-17",
"aBindings": [],
"oBindingsToRemove": {},
"mContexts": {},
"oData": [
{
"pipelinerunseq": 20547673299878010,
"starttime": "2024-03-05T13:05:38.693Z",
"stoptime": "2024-03-05T13:06:49.992Z",
"stagetype": "CompensateAndPay",
"status": "Successful",
"command": "PipelineRun",
"period": "June 2022",
"numerrors": 0,
"numwarnings": 2,
"time": "13:05",
"daydate": "05"
},
{
"pipelinerunseq": 20547673299878010,
"starttime": "2024-03-05T13:07:21.975Z",
"stoptime": "2024-03-05T13:08:04.098Z",
"stagetype": "CompensateAndPay",
"status": "Successful",
"command": "PipelineRun",
"period": "June 2022",
"numerrors": 0,
"numwarnings": 2,
"time": "13:07",
"daydate": "05"
}
],
"sDefaultBindingMode": "TwoWay",
"bDestroyed": false,
"bLegacySyntax": false,
"sRemoveTimer": null,
"iSizeLimit": 100,
"mSupportedBindingModes": {
"OneWay": true,
"TwoWay": true,
"OneTime": true
},
"mUnsupportedFilterOperators": {
"Any": true,
"All": true
},
"sUpdateTimer": null,
"bCache": true,
"aPendingRequestHandles": []
}

 

What shows in Dev Tools:

ronhagan_0-1713567046853.png

 

Ryan-Crosby
Active Contributor
0 Kudos
What is the script code you are executing to test out the answer that David provided?

Accepted Solutions (0)

Answers (3)

Answers (3)

Ron_Hagan
Product and Topic Expert
Product and Topic Expert
0 Kudos

So here's something odd.  When I use const oDataArray = oDrdModel.oData, I then created an IF statement checking whether oDataArray is an array or not using Array.isArray() and, for whatever reason, it says that it isn't an array.

That has me stumped.  I can see that it's an array.

Ron_Hagan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ryan,

The script is ToolPage.controller.js, which controls the ToolPage.view.xml view.

Or are you looking for the exact code?  This code retrieves the data shown in Dev Tools:

const oDtdModel = new JSONModel("../../rest/catalog/PipelineService");
console.log("oDtdModel: ", oDtdModel);

When I added the code that David suggested right after that, I got the error.  I tried a modified version of that code but that brought back something that didn't have the oData array in it.

const oData = oDtdModel.getData();
console.log("oData: ", oData);

The oData array of the one that shows everything in Dev Tools has 30 items in it, which is what I'm trying to get to.

Ron_Hagan_0-1713800516035.png

I hope that answered your question, Ryan.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Ron,

Change the following (it will only work if that is how you are already retrieving your model successfully) :

const oDtdModel = new JSONModel("../../rest/catalog/PipelineService");
console.log("oDtdModel: ", oDtdModel);

To:

const oDtdModel = new JSONModel("../../rest/catalog/PipelineService").getData();
console.log("oDtdModel: ", oDtdModel);

 

 

 

 

 

Regards,

Ryan Crosby 

david_bizer
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ron,

like this ?

let oData = this.getView().getModel("oDtdModel").getData();

This should give you your data as array, as seen in the dev tools.

Best regards,

David 

Ron_Hagan
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi David,
Ron_Hagan
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi David, I probably should have mentioned that I'm trying to get the data in ToolPage.controller.js. When I used this code, I got this error message: ModuleError: failed to execute module factory for ''sap/sf/monale/controller/ToolPage.controller.js'': this.getView is not a function I hope you still have an idea for me. 🙂