EXPOSE REST: How to output only selected JSON properties?

EXPOSE REST: How to output only selected JSON properties?

  

Web service methods - GET single record and GET all record list:

Use case: You want to optimise the JSON response output returned by outsystems expose REST response. You might want to do this when you return a large record list via mobile app (network is often slow and data is often very expensive!)

Solution: 

We added a “fields” input property.

"Fields" are comma-separated list of selected fields that tells the web service to only include the selected fields in the JSON response.

--------------

There are two work-arounds (that I could of right now)

Method #1) Create an extension to remove the JSON elements (fields) from the JSON output and return the JSON response with only the selected JSON elements (fields).

Method #2) Less productive way of solving this use case is by adding an if statement to an assign widget to every single structure attribute and remove the JSON elements prior to JSON response.


Method #3) ? Any better way to solve this use-case? 

Hi Robert,

I can think of another way but didn't test it.

If you have:

  1. an Output structure with all fields not mandatory 
  2. in your REST Service, set "Send Default Values" to "No"

And then, fill only the fields that you want to return.

In this case, I'm almost sure that OutSystems will only send the fields that were set (of not for their default value).

Does this work for you?

Nuno Maurício wrote:

Hi Robert,

I can think of another way but didn't test it.

If you have:

  1. an Output structure with all fields not mandatory 
  2. in your REST Service, set "Send Default Values" to "No"

And then, fill only the fields that you want to return.

In this case, I'm almost sure that OutSystems will only send the fields that were set (of not for their default value).

Does this work for you?


Nuno, that is correct! However the problem is all fields are filled programmatically by default when the fields property is empty and only when you specify the specific fields those fields are returned. 

I could add a bunch of if statements but that is a very time consuming and tedious task, on top of that if the attribute name changes there's going to be a problem, there's no property to get the structure's attribute name either which is another problem within itself.  ) 

Robert, in that case, I would go for method #1 you described above. 

Maybe someone else came up with a revolutionary method :)