Possibility to return JSON with empty list using a Message Definition

3
Dear Mendix enthusiasts, In my project I'm using message definitions in combination with export mappings to create a JSON responses for my published REST services (using the SVC template). For a GET method, the response I create consists of a key-value (long), collection name (string) and subsequently a list with n-items within that collection. Example: { ..."Key":2, ..."Name":"Product range", ..."Products":[ ......{ ........."Key":30, ........."UKitem":"EOS_002834" ......}, ......{ ........."Key":33, ........."UKitem":"EOS_009277" ......} ...] } When the collection however exists but does not contain items, the consuming system expects a response message that contains the fields ‘Key’, 'Name’ and the ‘Products’ list which is empty and therefore shows no items. Example: { ..."Key":2, ..."Name":"Product range", ..."Products":[ ...] } This however seems not possible, since the list-element in the response is only created by Mendix as soon as there actually is something on the list (i.e. by creating/retrieving a child object and setting the 0..*-association to the parent entity of the response message in the microflow that is responsible for building the response). My main question therefore is; how to create a JSON message that always contains the list element regardless the presence of child elements within that list? If I remembered correctly this was possible when using a JSON structure in Mendix to define your messages. The second question would be; what is actually considered best practice regarding whether or not having an ‘empty list’ in a JSON message. Thanks in advance for your help.
asked
2 answers
0

Hi David,

If you want to have the structure even when no items are present you could resort to a non persistent domain model for the purpose of creating the desired structure. This normally looks alot like your persisent domain model but gives you the freedom to design the response in the way you want. But beware, a lot of extra work because of need of custom mapping microflows from persistent to non persistent.

In your case I would add an attribute in the root object that contains the value/amount of the array of products. This way your consumers can read the value and then know if the need to process further or not. An example:

{
..."Key":2,
..."Name":"Product range",

...”NoOfProducts”: 0
}

 

{
..."Key":2,
..."Name":"Product range",

...”NoOfProducts”: 2,

..."Products":[
......{
........."Key":30,
........."UKitem":"EOS_002834"
......},
......{
........."Key":33,
........."UKitem":"EOS_009277"
......}
...]
}

 

answered
0

Hi Sjors,

Thanks for the reply. The addition of a field with the No. of Products is indeed a possible way to inform the receiving system that no list is to be expected. However I'm curious about your first option regarding the non persistent domain model. This is because I already have made non-persistent entities that essentially define the structure of my message. Using the message definition feature in Mendix I select these non-persistent entities which Mendix subsequently uses as a template for my JSON message structure. In the export mapping I select this message definition which then requires me to specify the entities with attributes that are used to fill out the fields of my message. All this is indeed a bit more cumbersome, but still doesn't allow me to always show the list in my message structure since the ‘list’ is a representation of the 0..1-association between the child entity(s) and the parent entity. Therefore no child(s) objects means no associations are defined hence no list in the response message is created. How should this still be possible according to your best knowledge using non-persistent entities when there are no child objects? 

answered