Fetch from other sources with multilevel json

Hi all,

After finding my way through the tutorials I'm trying to fetch data from a web service. 


The tutorials I've seen use web services with single level json. That works fine. But many real life web services use multi level json's where the top level is kind of a header and the content is in a loop below. 

Like this: 


My assumption is that if I connect the DataAction in the right way the combination of the screen's list object and the expression will point correctly at the sub-loop and will handle the looping accordingly. 



unfortunately the system pointed out quite clearly that my assumption was wrong


I tried numerous ways to jump a level (list of list etc) but so far I failed. Can anyone point me at the supposed way to solve this?

thanks!

Hi Martijn,

First, a "loop" is an iteration, not a list, but I assume you are talking about a list (the same for "sub-loop", that terminology doesn't make much sense to me)?

I'm also not exactly sure what you mean with "My assumption is that if I connect the DataAction in the right way the combination of the screen's list object and the expression will point correctly at the sub-loop and will handle the looping accordingly", but to answer the more generic question which seems to underly the above, the OutSystems Platform is perfectly capable of handling multi-level Structures and Lists in combination with REST (and JSON in general).

An error like the above is typically caused by Structures not defined properly, although there are REST services that produce inconsistent JSON (like returning an object if there's one item but an array if there's more) that the Platform can't handle without some additional programming.

I would advise you to increase the logging level of the REST API, so you can see what JSON is sent, and compare that to the structure of your Structures.

thanks for the link, I increased the logging. But it seems that the actual JSON equals the structure's structure.





Logic:




Interface:




The list is now connected to the output parameter, which is of type GetPlantOutputResponse.List


Now what I did was use an expression to show a value inside the data element. I can imagine this is wrong because I am iterating through the main level consisting of Sid/Dataunit/Data[] instead of through the level of Data[]


But I don't know how to do this. This is what I meant with that I expected that outsystems might have chosen the correct level to iterate through. But apart from that it still doesn't work even if I use the expression to display the sid field which is on the top level. And the structures are as far as I can see ok. 



I believe the solution might be changing the datatype of the Out1 parameter to DatumItem List. 



but that raises an error too.

Any clue?

Solution

Hi Martijn,

I can't escape the feeling that you're don't quite know what you're doing. What's done with the REST result on the Screen is of no importance at all, if the error is triggered by calling the REST service in the first place.

However, the problem is here:

You have specified the output as a List of the response type, instead of the response type itself. This is not correct of course, as the root isn't a list:


Solution

Well I never claimed I did.. this is the phase one goes through when playing around with a new platform. But I think your suggestion is exactly what's wrong. Thanks! I know it's one element, but I considered it in such a situation as a kind of a potential list that in this case happens to have only one element. Hence the data type. But I understand that was wrong. 



Works like a charm, again many thanks!

Good to hear it works, and glad I could be of help. Just remember that the mapping for JSON is that "{" starts an object is a single Structure, and "[" starts a list of objects is a List of Structure.