Skip to main content
Participating Frequently
February 15, 2021
Solved

Null conversion in a!tojson

  • February 15, 2021
  • 7 replies
  • 1 view

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.

Best answer by mikes0011

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"
)

7 replies

mikes0011
Brainy
February 15, 2021

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.

The Expression Guru
rambabubAuthor
Participating Frequently
February 15, 2021

Hi [mention:b45a4f6a20b14a008e4e0ec732a8bf98:e9ed411860ed4f2ba0265705b8793d05]

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)

mikes0011
mikes0011Answer
Brainy
February 15, 2021

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"
)

The Expression Guru