Page 1 of 1

Reading JSON

Posted: Fri Jul 31, 2020 3:41 pm
by jimwatterson
Hi

I'm trying to read a response back from a web service using

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

#UniCodeString := #Request.Response.AsString.Trim

#lDocument.LoadFromString( #UniCodeString )

#msgdta := #lDocument.RootNode<'MESSAGE'>.AsString


Which is great if I get valid json. However, in some circumstances the service returns a valid response (200) but the content is a HTML error page. When this hits the LoadFromString the application ends immediately with no error messages. There seems to be no error handling available in #PRIM_JSON.Document and the response is not null.

Does any one know how I can handle this properly.

(I'm using #PRIM stuff rather than the more widely documented #XPRIM because its much faster. In this case I could probably go with #XPRIM but I'd like to stick with this.)

Re: Reading JSON

Posted: Mon Aug 03, 2020 9:02 am
by BrendanB
Jim,

try something like:

Code: Select all

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

#UniCodeString := #Request.Response.AsString.Trim

#lDocument.LoadFromString( #UniCodeString )

if (#Document.isObject)    << returns false if not a JSON object...

#msgdta := #lDocument.RootNode<'MESSAGE'>.AsString

Endif

Or even see the value of #Request.Response.ContentType (if it doesnt contain JSON this should tell you).

Code: Select all


if (#Request.Response.ContentType.UpperCase.Contains('JSON'))

... do your doc load

else

... error message?

endif


Re: Reading JSON

Posted: Mon Aug 03, 2020 9:27 am
by jimwatterson
Thanks Brendan. Sage words!
Cheers
Jim