Page History
If you expect to see a JSON response from your HTTP request, it's recommended that you always verify that the HTTP response is indeed a well-formed JSON.
If you are using XPRIM_RandomAccessJsonReader (or XPRIM_JsonReader), you can check the error information returned when you set the source response (using SetSourceHttpResponse method).
The following example builds upon the example from the previous section. Notice that we know declare a component of type XPRIM_ErrorInfo, and pass it when we call the SetSourceHttpResponse method. We then check the OK property of the error info object to check if the JSON reader successfully reads the HTTP response body (which indicates that response is a well-formed JSON string)
...
Here is an example checking that JSON has been loaded into the JSON Document:
Define_Com Class(#PRIM_SRVM.HttpClientRequest) Name(#Req)
Define_Com Class(#PRIM_JSON.Document) Name(#JsonDoc) Reference(*DYNAMIC)
...
#Req.DoGet
...
Url('https://maps.googleapis.com/maps/api/geocode/json?...')* Check if request is successful
If (#Req.Response.IsSuccessHttpStatusCode)
* Set Place the JSON reader source fot response from the HTTP request
#Reader.SetSourceHttpResponse HttpResponse(#Req.Response) ErrorInfo(#ErrorInforesponse into PRIM_JSON.Document
#Req.Response.AsJson Result(#JsonDoc)
If_Ref Com(#JsonDoc) Is_Not(*NULL)
If (#ErrorInfo.OK)
* Read JSON values
* Good response. JsonDoc is JSON
else* Bad response. JsonDoc isn't valid
endifSimilar code can be used to validate if the structure of the document is correct. So, before the For/Endfor:
If_Ref Com(#JsonDoc.RootNode<'results'>) Is_Not(*NULL)
For Each(#Result) In(#JsonDoc.RootNode<'results'>)
Endfor
Endif