Null In JSON

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
jimwatterson
Posts: 39
Joined: Thu Jul 09, 2020 8:31 am

Null In JSON

Post by jimwatterson » Wed Oct 07, 2020 4:46 pm

Hi

I'm using

Define_Com Class(#PRIM_JSON.Document) Name(#lDocument)

to read a json object

For Each(#Child) In(#lDocument.RootNode)

#CONSTDID := #Child<'id'>.AsInt32
#RESTING := #Child<'resting'>.AsBoolean

endfor

Which works great until an element is missing, If there is no #Child<'resting'> the function crashes

I've tried the following but that crashes aswell

If Cond(*Not #Child<'resting'>.IsNull)
If (#Child<'resting'>.AsBoolean)
....
Endif
Endif

Any ideas?

MarkD
Posts: 691
Joined: Wed Dec 02, 2015 9:56 am

Re: Null In JSON

Post by MarkD » Thu Oct 08, 2020 9:53 am

Can you try:

If (#Child<'resting'> *isnot *null)

Also, each reference to #Child<'resting'> causes a lookup of the parsed JSON tree, so it might be quicker to execute using something like:

#TempRefererence <= #Child<'resting'>

Then use #TempReference instead of multiple #Child<'resting'> operations.

jimwatterson
Posts: 39
Joined: Thu Jul 09, 2020 8:31 am

Re: Null In JSON

Post by jimwatterson » Thu Oct 08, 2020 11:38 am

Thanks Mark

After a bit of experimenting we found that this works

#CATRTARST := 'N'
If Cond(#Child<'resting'> *IsNotEqualTo *null)
If (#Child<'resting'>.AsBoolean)
#CATRTARST := 'Y'
Endif
Endif

I think this is probably logically equivalent to your suggestion but the difference between all the types of null checking is too subtle for me!

Cheers
Jim

adale
Posts: 27
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: Null In JSON

Post by adale » Tue Sep 14, 2021 8:14 am

I have a similar issue.
The construct of the doc from the json worked fine until there was a missing element ( <'billTo'> in my case).
I have used suggestion here, and it does prevent my Server Module from crashing, but now I can not seem to get the element data when it is present.

Without the IF statement, the SM crashes.

But when there is data in the "billTo" element, I would like the fields to be populated.
What am I missing?


IF Cond(#Item<'billTo'> *IsNot *null)
* Bill-to
#W_FNAME := #Item<'billTo'>.ItemAt<1>.Asstring
#W_LNAME := #Item<'billTo'>.ItemAt<2>.Asstring
#W_COMP := #Item<'billTo'>.ItemAt<3>.Asstring
#W_ADDR1 := #Item<'billTo'>.ItemAt<4>.Asstring
#W_CITY := #Item<'billTo'>.ItemAt<5>.Asstring
#W_STATE := #Item<'billTo'>.ItemAt<6>.Asstring
#W_ZIP := #Item<'billTo'>.ItemAt<7>.Asstring
#W_CNTRY := #Item<'billTo'>.ItemAt<8>.Asstring
* End Bill-to
ENDIF

adale
Posts: 27
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: Null In JSON

Post by adale » Fri Sep 17, 2021 2:06 am

Follow up.
The IF cond statement worked to prevent the crash, and going back into this program group the next day, the data was getting populated from the Bill-To segment. I assume I must not have checked something in during my initial testing.

Post Reply