**free
//  WATSONTR2R:  This is a demo of Watson's Language Translator V3
//               using input/output with JSON documents.
//
//               Requires HTTPAPI and YAJL
//
//               This example uses YAJL's tree API to read the
//               response. For a DATA-INTO example, see WATSONTR3R
//
ctl-opt option(*srcstmt) dftactGrp(*no)
        bnddir('HTTPAPI':'YAJL');

/copy httpapi_h
/copy yajl_h

dcl-f WATSONTR2D workstn indds(dspf);

dcl-Ds dspf qualified;
   F3Exit ind pos(3);
end-Ds;

dcl-c UPPER 'ENESFRITPT';
dcl-c lower 'enesfritpt';

fromLang = 'en';
toLang   = 'es';

dou dspf.F3Exit = *on;

   exfmt screen1;
   if dspf.F3exit = *on;
      leave;
   endif;

   fromLang = %xlate(UPPER:lower:fromLang);
   toLang   = %xlate(UPPER:lower:toLang);
   toText = translate( fromLang: toLang: %trim(fromText) );

enddo;

*inlr = *on;
return;


dcl-proc translate;

   dcl-pi *n varchar(1000);
      fromLang char(2)       const;
      tolang   char(2)       const;
      fromText varchar(1000) const;
   end-pi;

   dcl-s url      varchar(2000);
   dcl-s request  varchar(2000);
   dcl-s response varchar(5000);
   dcl-s output   varchar(1000);
   dcl-s errMsg   varchar(500);
   dcl-s docNode  like(yajl_val);
   dcl-s node     like(yajl_val);

   yajl_genOpen(*off);
   yajl_beginObj();                     // {
     yajl_addChar('source': fromLang);  //   "source": "en",
     yajl_addChar('target': toLang);    //   "target": "fr",
     yajl_beginArray('text');           //   "text": [
       yajl_addChar(fromText );         //     "String here"
     yajl_endArray();                   //   ]
   yajl_endObj();                       // }

   request = yajl_copyBufStr();
   yajl_genClose();

   http_debug(*on: '/tmp/watson-diagnostic-log.txt');

   http_setOption('local-ccsid': '0');
   http_setOption('network-ccsid': '1208');

   http_setAuth( HTTP_AUTH_BASIC
               : 'apikey'
               : '< PUT YOUR API KEY HERE >' );

   url = 'https://gateway.watsonplatform.net/language-translator/api'
       + '/v3/translate?version=2018-05-01';

   monitor;
      response = http_string('POST': url: request: 'application/json');
   on-error;
      httpcode = http_error();
   endmon;

   docNode = yajl_string_load_tree(response: errMsg);
   if errMsg = '';
      node = yajl_object_find(docNode: 'translations');
      node = yajl_array_elem(node: 1);
      node = yajl_object_find(node: 'translation');
      output = yajl_get_string(node);
      yajl_tree_free(docNode);
   endif;

   return output;

end-Proc;
 