Skip to main content

I would like to deserialize JSON from a REST call into an array and I'm not sure of the exact syntax. Are there any examples you can point me to or can you provide the syntax? I am working with Visual COBOL 2.2 and Visual Studio 2013. Thanks!

This is what I have so far:

      $set sourceformat(variable)
      $set ilusing "System.Net"
      $set ilusing "Newtonsoft.Json"
      $set ilusing "System.Configuration"
      $set ilusing "System.Collections.Specialized" 

       class-id CallRestService implements type IDisposable.
       working-storage section.
       01  wc type WebClient value new WebClient().

       method-id RestService.
       local-storage section.
       procedure division.

         try   
               declare modelResult as object
               declare url as string
               set wc::UseDefaultCredentials to true;
               set wc::Headers[type HttpRequestHeader::ContentType] to "application/json"
               set serializedResult to wc::DownloadString(url)  *> Assume that url has been set to the appropriate route
               set modelResult to type JsonConvert::DeserializeObject(serializedResult) *> This is where I'm not sure how to put the result into an array
           catch ex as type Exception
               set serializedResult to ex       
          
           end-try
           goback.
       end method.

       method-id Dispose public.
       local-storage section.
       procedure division.
           goback.
       end method.

       end class.
      

I would like to deserialize JSON from a REST call into an array and I'm not sure of the exact syntax. Are there any examples you can point me to or can you provide the syntax? I am working with Visual COBOL 2.2 and Visual Studio 2013. Thanks!

This is what I have so far:

      $set sourceformat(variable)
      $set ilusing "System.Net"
      $set ilusing "Newtonsoft.Json"
      $set ilusing "System.Configuration"
      $set ilusing "System.Collections.Specialized" 

       class-id CallRestService implements type IDisposable.
       working-storage section.
       01  wc type WebClient value new WebClient().

       method-id RestService.
       local-storage section.
       procedure division.

         try   
               declare modelResult as object
               declare url as string
               set wc::UseDefaultCredentials to true;
               set wc::Headers[type HttpRequestHeader::ContentType] to "application/json"
               set serializedResult to wc::DownloadString(url)  *> Assume that url has been set to the appropriate route
               set modelResult to type JsonConvert::DeserializeObject(serializedResult) *> This is where I'm not sure how to put the result into an array
           catch ex as type Exception
               set serializedResult to ex       
          
           end-try
           goback.
       end method.

       method-id Dispose public.
       local-storage section.
       procedure division.
           goback.
       end method.

       end class.
      

We don't know what the JsonConvert class is - presumably it's in the Newtonsoft.Json namespace, but that's neither a .NET Framework namespace nor a Micro Focus one. So we don't know what the signature or semantics of JsonConvert::DeserializeObject are. So how can we answer the question?

And an array of what? Bytes? Characters? Something else? What's in the JSON response?

By the way, you can omit the local-storage section and procedure division lines, since the section is empty and the division header doesn't specify any parameters. I'd also recommend removing the period on the goback statements (in fact, I'd get rid of the gobacks entirely). And you can omit the "ex as type Exception" from the catch statement and for the catch body use "set serializedResult to exception-object".

And WebClient::DownloadString performs an HTTP GET (or an FTP RETR), which means there's no message-body in the request message; and therefore the Content-Type header is meaningless. Perhaps you meant to set the Accept header, which would tell the server that you're willing to receive JSON content; but the Accept header is restricted when using the WebClient class, so you can't do that. (The HttpWebRequest class lets you set the value of the Accept header in the request, but WebClient does not.)