Page 1 of 1

How to check if a JSON response element is NULL

Posted: Tue Nov 15, 2016 8:45 am
by davidbalansa
Hi all,

I have a VL Web application where I am getting the response back from a JSON request. It is possible for an element to have a value of null. In my RDMLX i am extracting the element with the following code:

#uUserDetails.upUserTitle := #Root<'title'>.AsString

When the value of #Root<'title'> is null, I get the following error:

Fatal Error:
{} can't be converted into a string

I am trying to condition my code to catch the null value. There is no intrinsic method isNull available on the element (#Root<'title'>.IsNull). I have also tried using:

if_ref com(#Root<'title'>) Is(*NULL)

But this evaluates to false. How can I test if the value of the element is null?

David

Re: How to check if a JSON response element is NULL

Posted: Tue Nov 15, 2016 10:07 am
by dannyoorburg
Hi David,

if ( 'title' ) is not supplied in your JSON, as in:

Code: Select all

{ 
   "name":"Danny",
   "age":42
}
then ( #Root<'title'> *is *NULL ) SHOULD evaluate to TRUE.


But if title IS supplied, but given the 'null' value

Code: Select all

{ 
   "name":"Danny",
   "age":42,
   "title": null 
}
then Visual LANSA ends up creating an instance of a JsonObject, where it probably should have created something like a JsonNull.

You can code around it for now by checking for the empty object that got created on behalf of the null-value, something like:

Code: Select all

If ((#JSON.RootItem<"title">.Type = Object) *AndIf (#JSON.RootItem<"title">.ItemCount = 0))

Endif
but I'm pretty sure it's a BUG, you should report it to LANSA support...

Regards,
Danny

Re: How to check if a JSON response element is NULL

Posted: Tue Nov 15, 2016 11:27 am
by davidbalansa
Hi Danny,

Thank you very much for the response. My scenario was:

{
"name":"Danny",
"age":42,
"title": null
}

Your code snipet:

If ((#JSON.RootItem<"title">.Type = Object) *AndIf (#JSON.RootItem<"title">.ItemCount = 0))
...
...
Endif

was able to capture the null value for the "title" element.

Thanks again,
David

Re: How to check if a JSON response element is NULL

Posted: Tue Nov 15, 2016 12:14 pm
by dannyoorburg
Hi David,

maybe you should code it

Code: Select all

If ((#JSON.RootItem<"title">.Type = Null ) *ORIF ((#JSON.RootItem<"title">.Type = Object) *AndIf (#JSON.RootItem<"title">.ItemCount = 0)))
...
Endif
so it doesn't fall over the moment you one day install an EPC that that contains a proper fix...

Regards,
Danny

Re: How to check if a JSON response element is NULL

Posted: Wed Nov 16, 2016 11:35 am
by davidbalansa
Hi Danny,

Thank you for going through the extra effort to future proof the code fragment.

Regards,
David