Force empty JSON structure in consumed REST service?
Question

Goodmorning all,

I have a problem with our customer in using a REST service. We consume this service. The endpoint requires we send one of 5 structures as an empty structure (it is being converted to XML, and the WSDL has a choice setting for the 5 structures). Sadly this is a structure required outside of the OutSystems domain and outside of my reach to change.

So for example if I have the following REST message structure:

{
"Attribute1": "text",
"Attribute2": "text2",
"Structure1": {"Attribute3": "text3"}
}

Let's say attribute 1 and 2 are filled, but 3 is not. Is it possible to send Structure1 empty? So like this:

{
"Attribute1": "text",
"Attribute2": "text2",
"Structure1": {}
}

I've not been able to do it, and I have only seen posts talking about this with structures defined as lists. I'm wondering if this is possible at all in the OutSystems platform.

So far I have tried filling Attribute3 with a dummy value and replacing the value with "" when sending it to the REST endpoint in the OnBeforeRequest using customized request output. However, this did not do the trick.

I hope someone has an idea. Thanks for any input and let me know if there are questions.

Kind regards,
Michiel van Lokven
OutSystems Consultant

HI Michiel van Lokven ,

Set the Default Value of Attribute3 as ""  and Send Default Value to No this will not send this attribute to the request payload if the value matches with default value


Now also set Send Default Value of Structure3 to Yes other wise it will not send the Structure3 as well in request Payload

                                                    


By changing both this properties of the Structure attributes I am getting below request payload  JSON

{\"Attribute1\":\"Test\",\"Attribute2\":\"Test2\",\"Structure1\":{}} 

Hope this will help you.

Best Regards

Devendra

Hi Devendra,

Thanks for your answer. I will try to test this method and will come back to you, have not thought about this yet. 

Regards,

Michiel

Hi Michiel ,

Ok fine let me know if you have any query.

Best Regards

Devendra

Hi Devendra,

Thanks, this seems to work. However, the situation is a bit more complex. We have a generic structure we use for 6 different processes, so something like:

{
"Attribute1": "text",
"Attribute2": "text2",
"Structure1": {
"SpecificStructure1":{attributes...},
"SpecificStructure2":{attributes...},
"SpecificStructure3":{attributes...},
"SpecificStructure4":{attributes...},
"SpecificStructure5":{attributes...},
"SpecificStructure6":{attributes...}}
}

Of these specific structures, only one can be present in the REST message. And if it is one, the basic structure as default value needs to be in it if is empty (so "SpecificStructure1":{} ).

So in a way I would need to have "Send Default Value" = yes, but it needs to be conditional. Any idea if that is possible to do somewhere?

The best I have come up with is to create 6 separate structures sadly, and use logic in the OS flow to determine which structure we need to send.

Thanks for any ideas.

Regards,
Michiel van Lokven

Hi,

So I've solved this by creating separate structures for each specific case where I needed to set a default structure to "yes", in my case 6 times. This meant 6 main structures, and 6 times the specific structure only containing one of the specific structures.

It also meant 6 times the same webservice call to the same endpoint to send any of these 6 messages. Sadly not very easy to maintain. 

The owners told me that in the new release of the webservice, they will fix this in another way, which will solve this problem eventually.

Thanks for the help so far Devendra, because I didn't know you could create an empty structure like this!

Regards,

Michiel van Lokven 

Hi Michiel,

I think this is possible with single structure as well you need to write some logic in OnBeforeRequest Rest event 

Some thing like  if SpecificStructure1":{} than replace with empty string 


Best Regards

Devendra

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.