Page 1 of 1

Null In JSON

Posted: Wed Oct 07, 2020 4:46 pm
by jimwatterson
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?

Re: Null In JSON

Posted: Thu Oct 08, 2020 9:53 am
by MarkD
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.

Re: Null In JSON

Posted: Thu Oct 08, 2020 11:38 am
by jimwatterson
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

Re: Null In JSON

Posted: Tue Sep 14, 2021 8:14 am
by adale
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

Re: Null In JSON

Posted: Fri Sep 17, 2021 2:06 am
by adale
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.