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?
Null In JSON
Re: Null In JSON
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.
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: 56
- Joined: Thu Jul 09, 2020 8:31 am
Re: Null In JSON
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
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
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
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
Arlyn Dale
Servias LLC
Servias LLC
Re: Null In JSON
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.
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.
Arlyn Dale
Servias LLC
Servias LLC