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
How to check if a JSON response element is NULL
-
davidbalansa
- Posts: 92
- Joined: Mon Feb 01, 2016 10:08 am
-
dannyoorburg
- Posts: 177
- Joined: Mon Jan 04, 2016 9:50 am
- Location: Australia
Re: How to check if a JSON response element is NULL
Hi David,
if ( 'title' ) is not supplied in your JSON, as in:
then ( #Root<'title'> *is *NULL ) SHOULD evaluate to TRUE.
But if title IS supplied, but given the 'null' value
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:
but I'm pretty sure it's a BUG, you should report it to LANSA support...
Regards,
Danny
if ( 'title' ) is not supplied in your JSON, as in:
Code: Select all
{
"name":"Danny",
"age":42
}But if title IS supplied, but given the 'null' value
Code: Select all
{
"name":"Danny",
"age":42,
"title": null
}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))
EndifRegards,
Danny
-
davidbalansa
- Posts: 92
- Joined: Mon Feb 01, 2016 10:08 am
Re: How to check if a JSON response element is NULL
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
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: 177
- Joined: Mon Jan 04, 2016 9:50 am
- Location: Australia
Re: How to check if a JSON response element is NULL
Hi David,
maybe you should code it
so it doesn't fall over the moment you one day install an EPC that that contains a proper fix...
Regards,
Danny
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)))
...
EndifRegards,
Danny
-
davidbalansa
- Posts: 92
- Joined: Mon Feb 01, 2016 10:08 am
Re: How to check if a JSON response element is NULL
Hi Danny,
Thank you for going through the extra effort to future proof the code fragment.
Regards,
David
Thank you for going through the extra effort to future proof the code fragment.
Regards,
David