[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: REST-JSON-YAJL



   Hi Tony,
   I have a example parsing JSON-data from OpenExchangeRates.org
   For test:
   1) You need HTTPAPI and YAJL from Scott Klement
   2) Take the HTTPAPI Command to Download the file from
   [1]http://openexchangerates.org/api/latest.json?app_id=yourid
        or take the JSON file at the appendix

   3) Look at the GETCURRAT program to parse the JSON data
   4) It is possible too to access a REST webservice with SQL since V7R1.
   This example is a access to Google Maps Webservice
   SELECT SYSTOOLS.HTTPGETCLOB
   ('[2]https://maps.googleapis.com/maps/api/geocode/json?address=Renntals
   tra%C3%9Fe%2020,74360%20Ilsfeld&components=country:DE','')
    From SYSIBM.SYSDUMMY1;
   Best regards
   Rainer
Rainer Ross
IT-Consulting
[3]www.myhofi.com - a smarter way to book hotels - powered by IBM i
Kaufering
Germany


   Am 25.04.2016 um 21:48 schrieb Tony Cesarios:

    [[4]cid:image001.gif@01D19F7F.48A941D0]

   Hi,


   Just wondering if anyone has a sample program using HTTPAPI to execute
   a GET from an external URL using REST and JSON.


   Had a look at YAJL as a solution.


   We need to go and retrieve orders which contain about 15 elements
   (parameters) and process them into our iSeries.



   Regards,

   Tony Cesarios


-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
[5]http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------

References

   1. http://openexchangerates.org/api/latest.json?app_id=yourid
   2. https://maps.googleapis.com/maps/api/geocode/json?address=Renntalstra%C3%9Fe%2020,74360%20Ilsfeld&components=country:DE
   3. http://www.myhofi.com/
   4. cid:image001.gif@01D19F7F.48A941D0
   5. http://www.scottklement.com/mailman/listinfo/ftpapi

Attachment: Currency_2016-04-25.js
Description: JavaScript source

         ctl-opt alloc(*teraspace) option(*nodebugio) bnddir('JSNUTL');         
      //------------------------------------------------------------------//    
      //                                                                  //    
      // JSON Parse OpenExchangeRates                                     //    
      //                                                                  //    
      //-----------------                                                 //    
      // R.Ross 02.2015 *                                                 //    
      //------------------------------------------------------------------//    
      // Files                                                            //    
      //------------------------------------------------------------------//    
                                                                                
         dcl-f   curcodp     disk    keyed usage(*update);                      
         dcl-f   curratp     disk    keyed usage(*update:*output);              
                                                                                
      //------------------------------------------------------------------//    
      // Copies                                                           //    
      //------------------------------------------------------------------//    
                                                                                
      /copy yajl/qrpglesrc,yajl_h                                               
                                                                                
      //------------------------------------------------------------------//    
      // Table Currencies                                                 //    
      //------------------------------------------------------------------//    
                                                                                
         dcl-s   d#count     uns(10) inz(*zero);      // CurrencyCounter        
                                                                                
         dcl-ds  t#cur       dim(200) qualified;      // CurrencyTable          
                  t#cur      like(crcur);             // Currency               
                  t#rate     like(crrate);            // Rate                   
                  t#rateur   like(crrate);            // Rate Euro              
         end-ds;                                                                
                                                                                
      //------------------------------------------------------------------//    
      // Variables                                                        //    
      //------------------------------------------------------------------//    
                                                                                
         dcl-s   h#errmsg    varchar(500);            // ErrorMessage           
         dcl-s   h#file      varchar(256);            // File                   
                                                                                
         dcl-s   h#doc       like(yajl_val);                                    
         dcl-s   h#node      like(yajl_val);                                    
         dcl-s   h#val       like(yajl_val);                                    
         dcl-s   h#key       varchar(50);                                       
                                                                                
         dcl-s   h#base      char(03);                // BaseCurrency USD       
         dcl-s   h#cur       char(03);                // Currency               
                                                                                
         dcl-s   h#basrat    like(crrate);            // BaseRate               
         dcl-s   h#eurrat    like(crrate);            // EuroRate               
         dcl-s   h#rate      like(crrate);            // CurrencyRate           
                                                                                
         dcl-s   h#sec       uns(10);                 // Seconds                
         dcl-s   h#date      date;                    // Date                   
                                                                                
         dcl-s   h#ind       int(10) inz(*zero);      // Index                  
         dcl-s   h#count     int(10) inz(*zero);      // Counter                
                                                                                
      //------------------------------------------------------------------//    
      // Main                                                             //    
      //------------------------------------------------------------------//    
                                                                                
           exsr main;                                                           
                                                                                
           *inlr = *on;                                                         
      //------------------------------------------------------------------//    
      // Main                                                             //    
      //------------------------------------------------------------------//    
         begsr main;                                                            
                                                                                
           clear t#cur;                               // CurrencyTable          
                                                                                
           h#file = '/tmp/Currency_2016-04-25.js';                              
                                                                                
           h#doc = yajl_stmf_load_tree(h#file:h#errmsg);                        
                                                                                
           if h#doc <> *null;                                                   
              h#base = yajl_get_string(yajl_object_find(h#doc:'base'));         
              h#sec  = yajl_get_number(yajl_object_find(h#doc:'timestamp'));    
              h#date = getdate(h#sec);                                          
                                                                                
              h#node   = yajl_object_find(h#doc:'rates');                       
                                                                                
              dow yajl_object_loop(h#node:h#ind:h#key:h#val);                   
                h#cur    = h#key;                                               
                h#rate   = yajl_get_number(h#val);                              
                d#count += 1;                                                   
                t#cur(d#count).t#cur  = h#cur;                                  
                t#cur(d#count).t#rate = h#rate;                                 
                select;                                                         
                 when h#cur = 'EUR';                                            
                   h#eurrat = h#rate;                                           
                 when h#cur = h#base;                                           
                   h#basrat = h#rate;                                           
                endsl;                                                          
              enddo;                                                            
                                                                                
              wrtcurrat(%addr(t#cur):d#count:h#date); // Write CurrencyRates    
           endif;                                                               
                                                                                
         endsr;                                                                 
      //------------------------------------------------------------------//    
      // Procedure - Write Currency Rates into CURRATP                    //    
      //------------------------------------------------------------------//    
         dcl-proc wrtcurrat;                                                    
         dcl-pi *n;                                                             
                 ##array_p   pointer       const;     // ArrayPointer           
                 ##count     like(d#count) const;     // Counter                
                 ##date      like(h#date)  const;     // Date                   
         end-pi;                                                                
                                                                                
         dcl-ds  p#cur       likeds(t#cur) based(##array_p);                    
                                                                                
         dcl-s   p#fact      like(crrate);            // Factor                 
         dcl-s   p#ind       int(10) inz(*zero);      // Index                  
                                                                                
           eval(h) p#fact = h#basrat / h#eurrat;      // Factor = Base/Euro     
                                                                                
           for p#ind = 1 to ##count;                                            
             eval(h) t#cur(p#ind).t#rateur = t#cur(p#ind).t#rate * p#fact;      
                                                                                
             cdcur  = t#cur(p#ind).t#cur;             // Currency               
                                                                                
             chain (cdcur) curcodf;                                             
                                                                                
             cdrate   = t#cur(p#ind).t#rateur;        // Currency-Rate          
             cdcurdat = ##date;                       // Currency-Date          
                                                                                
             if %found(curcodp);                                                
                update curcodf;                                                 
             endif;                                                             
                                                                                
             crcur  = t#cur(p#ind).t#cur;             // Currency               
             crdate = ##date;                         // Currency-Date          
                                                                                
             chain (crcur:crdate) curratf;                                      
                                                                                
             crrate = t#cur(p#ind).t#rateur;          // Currency-Rate          
                                                                                
             if %found(curratp);                                                
                update curratf;                                                 
              else;                                                             
                write  curratf;                                                 
             endif;                                                             
           endfor;                                                              
                                                                                
         end-proc;                                                              
      //------------------------------------------------------------------//    
      // Procedure - Get Date from Unix Timestamp                         //    
      //------------------------------------------------------------------//    
         dcl-proc getdate;                                                      
         dcl-pi *n           like(p#date);            // Date                   
                 ##sec       uns(10) const;           // Seconds                
         end-pi;                                                                
                                                                                
         dcl-s   p#date      date;                    // Date                   
         dcl-s   p#stmp      timestamp;               // TimeStamp              
                                                                                
           p#stmp = %timestamp('1970-01-01-00.00.00.000000'); // UnixTime       
                                                                                
           return %date(p#stmp + %seconds(##sec));    // UnixTime + Seconds     
                                                                                
         end-proc;                                                              
      //------------------------------------------------------------------//     
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------