Force JSON serialize of an empty array

Hi,

I have a structure I'm trying to deserialize into JSON to send elsewhere. 

Some of the fields in this structure are lists of objects. When <List>.Empty == true,  Outsystems supposedly initializes a single empty object with all default values. This is what I want to serialize.

eg:

{
  "list1": [
    {
      "atr1": "",
      "atr2": ""
    }
  ],
  "list2": [
    {
      "atr1": "",
      "atr2": ""
    }
  ]
}

When I put this into JSON.Serialize, I get an empty list in the output rather than a list with an empty object in it.

eg:

{
  "list1": [],
  "list2": []
}

I notice I could specify JSON.Serialize to target ParentObject.SubObject.Current, which seems to get me my empty object, but only the one I specify.

eg.

{
  "list1": [],
  "list2": [
    {
      "atr1": "",
      "atr2": ""
    }
  ]
}

I want to serialize all of them, and I want to avoid creating a bunch of empty local variables and adding them into the structure manually.

Is it possible to force the JSON.Serialize action to serialize the empty object inside this list? Is it possible to change the 'empty' attribute somehow? (it says it's read-only). 

Hi,

You can select "Serialize Default Values" to Yes in JSONSerialize action.

Regards.


Prasad Rao wrote:

Hi,

You can select "Serialize Default Values" to Yes in JSONSerialize action.

I am already using this to get the other fields; it does not work on lists.


Hi,

As per my understanding, default Outsystems serialize or ardoJSON won't help you. Need to try other plugins in forge if any of them helps, my bet is that you might have to initialize it with empty object or hard code the JSON string.

Regards.


Hi MichaelR,

The default value for a list is an empty list.

So what you want cannot be achieved without any additional coding.

Regards,

Daniel

Daniël Kuhlmann wrote:

Hi MichaelR,

The default value for a list is an empty list.

So what you want cannot be achieved without any additional coding.

Regards,

Daniel

I'm pretty sure there is an empty object sitting there in memory since in code you can still reference fields of List.Current of an empty list - and also the description of the 'empty' variable of a structure is:

So I was really hoping there was a way of forcing the serializer to process the empty element. Can I bypass the 'read only' on the Empty property programmatically somehow to set it to True?

Hi again,

You are talking about the OutSystems list that has a current object. I was talking about JSON.

OutSystems specifically created the current object on purpose to be available also of the list is empty. In most languages a list that is empty is truly empty, like also in a JSON object.

As I already said what you are looking for does not work like that in OutSystems. The current object will not be represented in the JSON.

Regards,

Daniel