Null conversion in a!tojson

A Score Level 1

For my service integration, i have to send the request in json format.

I am converting my nested CDT into json format by using a!tojson function.

Here the issue is my service has some optional input parameters and when i am passing null for those parameters, a!tojson is converting them to "" (double quotes).

My service is not accepting the "", i should pass Null.

a!toJson(
  'type!{urn:com:appian:types}_testCDT'(
    name: "ram",
    age: Null,
    place: ""
  )
)

json looks like :

{

"sampleName"  : "Test",

"Organization" : {

"Name" : "TestName",

"Branch" : ""

}

}

here Branch value should be Null instead of "".

Thanks in advance.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    After a quick test I can confirm that a!toJson() sets the value to "null" if it's passed a literal null, and only to empty text ("") when empty text is passed.  I'm not sure what data you're feeding it, but you might need to first consider whether the data itself is quite correct.

  • 0
    A Score Level 1
    in reply to Mike Schmitt

    Hi

    Thank you for the response.

    I am trying to type-cast the CDT and converting to json. 

    a!toJson(
      'type!{urn:com:appian:types}_testCDT'(
        name: "ram",
        age: Null,
        place: ""
      )
    )

    All fields are having the datatype as Text in CDT.

    I am getting the response like

    "{"name":"ram","age":"","place":""}"(Text)

  • +1
    Certified Lead Developer
    in reply to Ram

    I can confirm this is how it behaves when using a typecast CDT.  Have you considered using a Regular Expression to replace empty quotes in the resulting JSON string with NULL literals prior to passing it to your external service?  That seems like a bit of a workaround but it might be better than continuing to spin your wheels dealing with Appian's inconsistent treatment of NULL values.

    i.e. something like this:

    regexreplaceall(
      pattern: "(:)""""",
      searchString: a!toJson(
        'type!{urn:com:appian:types:TEST}TEST_testCdt'(
          id: null(),
          date: null(),
          createdBy: "",
          modifiedBy: null()
        )
      ),
      replacementString: "$1NULL"
    )

Reply
  • +1
    Certified Lead Developer
    in reply to Ram

    I can confirm this is how it behaves when using a typecast CDT.  Have you considered using a Regular Expression to replace empty quotes in the resulting JSON string with NULL literals prior to passing it to your external service?  That seems like a bit of a workaround but it might be better than continuing to spin your wheels dealing with Appian's inconsistent treatment of NULL values.

    i.e. something like this:

    regexreplaceall(
      pattern: "(:)""""",
      searchString: a!toJson(
        'type!{urn:com:appian:types:TEST}TEST_testCdt'(
          id: null(),
          date: null(),
          createdBy: "",
          modifiedBy: null()
        )
      ),
      replacementString: "$1NULL"
    )

Children