DATA-INTO Error

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
markl
Posts: 6
Joined: Mon Jan 20, 2025 9:16 pm

DATA-INTO Error

Post by markl »

This is a follow up post to the issue I was having with HTTPAPI. That issue has been resolved.

I have a simple authorization routine. I copied much of the code from the recent OATH2 post. I POST a clientId and clientSecret to a webservice and it returns an expires variable and an access token variable in JSON. I'm trying to use the DATA-INTO procedure to parse the JSON.

I'm able to retrieve the resultstr vaiable and it appears to be formatted correctly. When the code hits the DATA-INTO code, I get the error: "The document for the DATA-INTO operation does not match the RPG variable". I've reviewed the related posts but haven't found an answer. Can anyone help?

I believe I have the qualifying variables set up correctly to match the return string.

resultStr displays as: {"Expires":180,"AccessToken":"xxxx... ...xxx"}

Code: Select all

H DFTACTGRP(*NO)                                     
H BNDDIR('HTTPAPI':'YAJL')                           
 /include yajl_h                                     
 /copy httpapi_h                                     
                                                     
  dcl-s clientid       varchar(4096) inz(            
    'xxxxxxxxxxxxxxxxxxxxxxx');             
  dcl-s clientsecret   varchar(4096) inz(            
    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$');  
                                                     
  dcl-s sendData       varchar(16384);               
                                                     
  dcl-ds authResult qualified;                       
    Expires          int(3);                              
    AccessToken varchar(4096);                       
  end-ds;                                            
                                                     
  dcl-s resultStr      varchar(16000000);            
  d msg             s             52A                                                  
  d rc              s             10I 0                                                
                                                                                     
     http_debug(*on);                                                                
     *inlr = *on;                                                                    
                                                                                     
     //                                                                              
     //  Build the variables                                                         
     //                                                                              
                                                                                     
     sendData = 'clientId=' + http_urlEncode(clientid)                               
              + '&clientSecret=' + http_urlEncode(clientsecret);                     
                                                                                     
     //                                                                              
     //  post the credentials to retrieve the token                                  
     //                                                                              
                                                                                     
     rc = http_req( 'POST'                                                           
      :'https://xxxx.com/api/authenticate'              
              : *omit  
              : resultStr // String to receive the results              
              : *omit                                                   
              : sendData                                                
              : 'application/x-www-form-urlencoded');                   
                                                                        
    //                                                                     
    // error checking                                                      
    //                                                                     
                                                                        
    if (rc <> 1);                                                          
      msg = http_error();                                                  
      dsply msg;                                                           
      return;                                                              
    else;                                                                  
      msg = resultStr;                                                     
      dsply msg;                                                           
      data-into authResult %DATA(resultStr)                                
                        %PARSER('YAJLINTO');                            
    endif;    
    return;   
 
jonboy49
Posts: 239
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-INTO Error

Post by jonboy49 »

You need to add 'case=any' as an option. Like so:

Code: Select all

data-into authResult %DATA(resultStr : 'Case=any')                                
                        %PARSER('YAJL/YAJLINTO');           
You have to tell RPG to convert the case of the elements in the JSON before it attempts to match them to the RPG fields. RPG upper-cases all variable names so right now it is attempting to match the RPG field 'EXPIRES' with the JSON 'Expires' and they don't match.

P.S. When handling some JSON docs you will need to use case=convert to deal with non-RPG characters in JSON names.
markl
Posts: 6
Joined: Mon Jan 20, 2025 9:16 pm

Re: DATA-INTO Error

Post by markl »

Issue resolved. Thank you!

- markl
Post Reply