Legacy Dev Forum Posts

 View Only

Sign Up

How to omit empty/null optional fields

  • 1.  How to omit empty/null optional fields

    Posted 06-05-2025 18:41

    mk_telephony | 2019-03-28 07:49:49 UTC | #1

    We have defined a data action (web services integration) with the following contract: Input Contact

        {
          "title": "Test Flow",
          "description": "Makes post request",
          "type": "object",
          "required": [
            "conversation_uuid"
          ],
          "properties": {
            "city_id": {
              "anyOf": [
                {
                  "type": "integer"
                },
                {
                  "type": "null"
                }
              ]
            },
            "conversation_uuid": {
              "type": "string"
            },
            "job_uuid": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ]
            }
          }
        }

    In the configuration to make the POST request, the request body template is as follows:

    {"conversation_uuid":"${input.conversation_uuid}""city_id":$!{input.city_id},"job_uuid":"$!{input.job_uuid}"}

    However, if the input.jobuuid is null, the silent formal notation sets the value to an empty string (""), resulting in validation to fail in the called service since an empty string is not a valid uuid. Similarly, if input.cityid is null, an empty value is set resulting in malformed json:

    "message": "Request body is malformed. Error: Unexpected character (',' (code 44)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: (String)\"{\"conversation_uuid\":\"87201862-fd7a-4ce4-8967-4d1c26ecf221\",\"city_id\":,\"job_uuid\":\"null\"}\"]",

    I tried to use the successTemplateUtils.firstFromArray to try to set non-empty string defaults:

        {
          "conversation_uuid": "${input.conversation_uuid}",
          "city_id":${successTemplateUtils.firstFromArray("[$!{input.city_id}]","null")
          },
          "job_uuid": "${successTemplateUtils.firstFromArray("[$!{input.job_uuid}]","null")}"
        }

    But the above does not work either. Additionally, the called service has cityid and jobuuid as optional fields but does not allow them to be null.

    Is there a way to define the request body template such that certain properties are omitted if they are null/unset or empty string?

    I have looks at the velocity macros data actions guide, but it does not seem to have a way to do the above.

    Any help would be appreciated.


    Jason_Mathison | 2019-03-28 14:00:12 UTC | #2

    Hi mk_telephony,

    I reproduced the behavior you described. I will submit an internal ticket for this, but I don't know if or when we will fix this behavior. Our expectation for the input values to Data Actions was for very simple JSON. As you probably noted the test mode UI does not support the anyOf capability you are using.

    As an immediate workaround you might try making your request template something like this: { ${input.cityid} ${jobuuid} "conversationuuid": "${input.conversationuuid}" } and building cityid and jobuuid inside of architect.

    --Jason


    Jason_Mathison | 2019-03-28 20:12:17 UTC | #3

    After looking at a couple of web pages I think I have a solution: Checking for null: https://wiki.apache.org/velocity/CheckingForNull If/Then/Else syntax: http://people.apache.org/~henning/velocity/html/ch05s03.html

    "requestTemplate": "{ #if( $input.cityid) \"cityid\": ${input.cityid}, #end #if($input.jobuuid) \"jobuuid\": \"${input.jobuuid}\", #end \"conversationuuid\":\"${input.conversationuuid}\"}",

    This omits the entire "foo" : "bar" for the optional variables if they are null.

    --Jason


    Jason_Mathison | 2019-04-01 16:26:09 UTC | #4

    Did this resolve your issue?


    mk_telephony | 2019-04-02 05:07:36 UTC | #5

    Hey Jason,

    Yes, the suggestion using velocity macros resolved the issue.

    Thanks for the prompt reply.


    system | 2019-05-03 05:10:44 UTC | #6

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 4881