How to check if a JSON response element is NULL

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
davidbalansa
Posts: 47
Joined: Mon Feb 01, 2016 10:08 am
Location: Australia

How to check if a JSON response element is NULL

Post by davidbalansa » Tue Nov 15, 2016 8:45 am

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

dannyoorburg
Posts: 152
Joined: Mon Jan 04, 2016 9:50 am
Location: Australia

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

Post by dannyoorburg » Tue Nov 15, 2016 10:07 am

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

davidbalansa
Posts: 47
Joined: Mon Feb 01, 2016 10:08 am
Location: Australia

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

Post by davidbalansa » Tue Nov 15, 2016 11:27 am

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

dannyoorburg
Posts: 152
Joined: Mon Jan 04, 2016 9:50 am
Location: Australia

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

Post by dannyoorburg » Tue Nov 15, 2016 12:14 pm

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

davidbalansa
Posts: 47
Joined: Mon Feb 01, 2016 10:08 am
Location: Australia

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

Post by davidbalansa » Wed Nov 16, 2016 11:35 am

Hi Danny,

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

Regards,
David

Post Reply