Reading 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: 56
Joined: Thu Jul 09, 2020 8:31 am

Reading JSON

Post 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.)
BrendanB
Posts: 134
Joined: Tue Nov 24, 2015 10:29 am

Re: Reading JSON

Post 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

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

Re: Reading JSON

Post by jimwatterson »

Thanks Brendan. Sage words!
Cheers
Jim
Post Reply