Hi Team,
I'm trying to perform JSON serialization, but it's not working as expected. I have a structure with attributes like IsSelect and IsCheck, which I am assigning as False. However, after serialization, my JSON string is coming out empty.
Thanks in advance for your help!
Output at UI-
Hi Rahul,
Did you turn on the Serialize Default Values property? Boolean False value is considered default.
Example:
Hi @Mihai Melencu ,Thank you for your reply. I don't want to serialize default values. I only want to serialize the attributes that I explicitly assign, whether the value is false or true.
Hi, in that case you can set the attributes that you need as mandatory. This will ensure the attribute is always included.
However, that's not the case for my situation. I need to send this JSON in an API request, and sometimes I need to include this structure in the request, while other times I do not. If I set "IsMandatory" to "Yes," the object is always included in the JSON, which is not what I want.
Another issue is that it's now requiring a value throughout the application, which causes errors in places where it was already being used.
JSON serialization was working properly before, but this issue has started happening recently.
@Rahul Kumar — I understand how the default behavior of JSON serialization in OutSystems can cause unexpected issues, especially when working with optional fields in API payloads.
In my opinion, one of the cleanest and most maintainable workarounds is to use two separate structures:
1.One structure that includes the optional field(s) or nested object(s)
2.Another structure that excludes them completely
This approach gives you full control at design time and allows you to decide at runtime which structure to populate and serialize based on your logic.
This approach isn’t the cleanest or most maintainable.
In the driver module, I have a main structure that contains many nested structures. If we create separate structures just for this purpose, it will result in many duplicate structures with similar names (e.g., FirstAttr2). Since multiple developers are working on this, it will be difficult for them to identify which structure to use. For example, if someone needs to add a new field, they won’t know which duplicate structure to update or understand the purpose of the others. I’m already aware of this workaround, but it introduces confusion and maintainability issues.
What you want is a tri-state: not assigned, True or False. This is simply not possible with OutSystems out of the box, and has never been possible. Regardless of what you claim, there hasn't been any change in how OS handles these things. On a low level, the Platform does not keep track of whether a value has been explicitly assigned or not.
The only way to handle this is change the Boolean to a Text, and in the On Before Request manipulate the JSON so that if the Text is empty, you remove the attribute completely, and if it's "True" or "False" you change it to true or false respectively.
Dourine Boudry wrote a very good article on implementing a Trilean. See here.
Hi Rahul Kumar,
It's totally depends on what is the Default Value of structure's Attribute, If the default value of structure's attribute is True it will not be appear in JSON serialize string for true. and same as for false as you have mentioned in your query.
if you want to get JSON serialize string in all the cases you have to select YES for JSONSerialize Default Value.
The OP explained that that is not his problem, and others have already answered his question. Please read questions carefully, and also other answers, before replying.