Json Serialize false value
Application Type
Traditional Web

Hi there! 


I have the following scenario, in a structure I have a Boolean property, which depending on a condition, it must be sent in json to a REST API, but in other cases not, below how this property is defined in the structure.

In cases where I do not need this property not to be serialized, this current configuration works, but in cases where I need to send this property with a false value, this configuration does not work, has anyone been through this and how did it resolve?

Obs: I can't leave it as mandatory


mvp_badge
MVP

Hi Janerson,

There are two properties that may help you to understand when an attribute is send or not.

The SendDefaultValue property, if defined as Yes, will send an attribute with the DefaultValue defined by you, even if it is not filled before.

Since you SendDefaultValue property is defined as No, it will only send the values if you fill it in an assign for example.

You can use the condition you mentioned in an if and assign this attribute incidenciaPrevidenciaria to True or False for example.


Hope it Helps,

RR :)

So, this is not happening, I believe it is a bug, making the debug I noticed that the structure is being filled, the problem happens when I pass this structure to the POST_PESSVERBAS action, until the point 1 of the image I have this property incidenciaPrevidenciaria , from point 2, this property no longer exists, I believe it is a bug in serialization 


when the condition is 1, I need the json to be sent that way


when the condition is 2, I need the json to be sent that way, in the current configuration of the structure, if I do not fill the incidenciaPrevidenciaria property the following json is sent correctly

but when the condition is equal to 1 and the incidenciaPrevidenciaria property has a false value, it is not serialized, and the resulting json is the same as in the image above

Are you assigning "false" to this attribute. If not then it will not send.

Just assign "false" when you want to send. Btw Raphael Ranieri provided detailed answer. :)


yes, I'm assigning false value to the incidenciaPrevidenciaria property

Hi,

 
Has this issue been resolved? I'm running into exactly the same problem and the answers posted do not seem to help.

In my case, I even made my boolean mandatory and it still does not serialize correctly.

Regards,

Michiel

Nulls vs zeros / default values just sent some greetings.

I did a quick demo on behaviour:

All methods have output "BooleanValue" structure. This is a structure with two boolean attributes, one having default value set as "False" and other where default value is "True". Method names will give you a hint of what is assigned inside that method.

Calling these API methods yield following results - basically whenever "Send default values" property of exposed REST API is set to "No", default value is omitted from serialization (and treated as if it's NULL value in receiving end). If "Send default values" property of exposed REST API is "Yes", everything gets serialized despite being assigned or not.

Results:

DontSerializeDefaults/SerializationTest_BothFalse: 

{"Default_TrueBoolean":false}

DontSerializeDefaults/SerializationTest_BothTrue:

{"Default_FalseBoolean":true}

DontSerializeDefaults/SerializationTest_BothUnset:

{}

SerializeDefaults/SerializationTest_BothFalse:

{"Default_FalseBoolean":false,"Default_TrueBoolean":false}

SerializeDefaults/SerializationTest_BothTrue:

{"Default_FalseBoolean":true,"Default_TrueBoolean":true}

SerializeDefaults/SerializationTest_BothUnset:

{"Default_FalseBoolean":false,"Default_TrueBoolean":true}

To avoid creating and maintaining multiple structures, only way I can figure how to handle the case defined in initial post is to:

  1. serialize structure, including default values
  2. remove unwanted key/value pair(s) from serialized JSON based on some logic (=text manipulation)
  3. send this modified JSON by setting REST API method output type to 'Text'.

br,

-Mikko(N)

Hi Jenerson,

I had the same scenario before and I had to create 2 separate structures. for your case, 1 with incidenciaPrevidenciaria and another without. Use the appropriate structure depending on the condition.

-Cezar

Also don't forget to set the "Send Default Value" property of incidenciaPrevidenciaria to True.

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