Reading JSON Problem(s)

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/
emhill
Posts: 43
Joined: Thu Jul 29, 2021 1:15 pm

Reading JSON Problem(s)

Post by emhill »

I am completely new to YAJL. I am needing some help on reading a JSON document. I have written out to the IFS with no issues but reading is giving me problems. Here is the simple .json I am attempting to read:

===============

Code: Select all

{
  "status": "Success",
  "message": "Record Retrieved Successfully",
  "interfaceName": "I-207 - Batch Claim Submittal Response",
  "batchClaimResponse": [
    {
      "businessPartnerCode": "0322400000",
      "claimInfo": {
        "claimNumber": "M4895710",
        "trackingNumber": "M023434499",
        "repairingOutlet": "0322400006",
        "repairOrder#": "227533",
        "serialNumber": "6610509335",
        "submittalIndicator": "I",
        "dateTimeProcessed": "2020-02-05T12:00:00.000",
        "claimStatusCode": "30",
        "claimStatusDescription": "Pending Approval",
        "dispositionCode": "",
        "dispositionCodeDescription": "",
        "rmaStatusCode": "",
        "rmaStatusDescription": "",
        "approvedDollarAmount": "",
        "identifier": "12345",
        "claimProcessMsgInfo": [
          {
            "lineNumber": "1",
            "processCode": "1371",
            "processCategoryDescription": "CLAIM",
            "processMessage": "AT: Travel requires manual review."
          },
          {
            "lineNumber": "2",
            "processCode": "1403",
            "processCategoryDescription": "NET ITEMS",
            "processMessage": "AT: Net Item exceeds auto-approval limit in table"
          }
        ]
      }
    }
  ]
}
===============

Here are my RPG data structures which I think may be part of the problem:

===============

Code: Select all

d response_t      ds                  qualified                 
d                                     template                  
d partnrcode                    10a                             
d claiminfo                           likeds(claiminfo_t)       
d process                             likeds(process_t) dim(999)
                                                                
d claiminfo_t     ds                  qualified                 
d                                     template                  
d  claim#                       20a                             
d  track#                       10a                             
d  outlet                       10a                             
d  ro                            6a                             
d  serial#                      20a                             
d  sind                          1a                             
d  datetime                     23a                             
d  stscode                       4a                             
d  stsdesc                      20a                             
d  dispcode                     10a                             
d  dispdesc                     20a                             
d  rmacode                       3a                             
d  rmadesc                      80a                             
d  appvdolr                     20a                             
d  identifier                   20a                             

d process_t       ds                  qualified                  
d                                     template                   
d lineno                         5a                              
d prccode                        4a                              
d catgdesc                      20a                              
d prcmesg                       80a                              
                                                                 
d result          ds                  qualified                  
d success                        1n                              
d errmsg                       500a                              
d response                            likeds(response_t) dim(150)
===============

I can get the "batchClaimResponse" array started ok using this:

response = yajl_object_find(docNode: 'batchClaimResponse');

and then looping using dow yajl_array_loop( response: i: node);

I get the "businessPartnerCode" ok but it is the section after that ("claimInfo") where I run into trouble. I do not know how to grab all those elements: "claimNumber", "trackingNumber", repairingOutlet", etc. I thought maybe the yajl_object_loop() but was not sure how to define the parameters to that procedure.

Any help would be appreciated and I can provide any more information you need.

Sorry about the code. I do not have RDI....

Thanks in advance!
jonboy49
Posts: 207
Joined: Wed Jul 28, 2021 8:18 pm

Re: Reading JSON Problem(s)

Post by jonboy49 »

If you are not that familiar with the workings of YAJL then why not make life simpler for yourself and use DATA-INTO with the YAJLINTO parser? You've already got the DS (or close to it) so it should be easy.

If you are not familiar with DATA-INTO I have written several articles on the topic and most are here: https://authory.com/JonParisAndSusanGan ... &view=text

In that list you'll also find "An RPGers First Steps with JSON" which covers using raw YAJL and should show you how to proceed - but I highly recommend that you "discover" DATA-INTO.

P.S. To include code samples use the code /code tags in [ ] brackets. It should show up as the </> icon in the edit bar in your browser when you enter your question. Works like this:

Code: Select all

abd
   xysz
emhill
Posts: 43
Joined: Thu Jul 29, 2021 1:15 pm

Re: Reading JSON Problem(s)

Post by emhill »

Thanks Jon! I tried a little scaled down version. Here is my RPG:

Code: Select all

**free                                                    
 ctl-opt dftactgrp(*no) debug option(*srcstmt:*nodebugio);
 ctl-opt bnddir('QC2LE':'YAJL');                          
                                                          
 /include yajl_h                                          
                                                          
 dcl-ds claimresponse dim(150) qualified;                 
    partnercode char(10);                                 
       dcl-ds claiminfo;                                  
          claim# char(20);                                
          track# char(10);                                
          repairout char(10);                             
          ro char(6);                                     
          serial# char(10);                               
          subind char(1);                                 
          datetime char(23);                              
          statuscd char(3);                               
          statusds char(80);                              
          dispcode char(10);                              
          dispdesc char(80);                              
          rmacode  char(3);                               
          rmadesc  char(80);                              
          apprdolr char(15);
          identifier char(10);                            
        end-ds;                                            
        dcl-ds process dim(300);                           
           lineno char(10);                                
           proccode char(4);                               
           procdesc char(25);                              
           procmsg  char(80);                              
        end-ds;                                            
     end-ds;                                               
                                                           
     dcl-s jsonpath varchar(150)                           
        inz('/eric/4WARD/Claim Submit Response Test.json');
                                                           
     data-into claimresponse %data(jsonpath:               
                             'doc=file case=any')          
                             %Parser('YAJL/YAJLINTO');     
                                                           
     *inlr = *on;                                                                        
It compiles with no problem but when I run it, I get the following error:

"Cannot resolve to object yajlinto."
"The program or procedure is not available for the DATA-INTO operation. Reason code 1."

Reason 1 = The program or the procedure in the service program was not found.

I have YAJL in my library list and if I do a WRKOBJ from the command line, YAJLINTO pops right up. Suggestions?
jonboy49
Posts: 207
Joined: Wed Jul 28, 2021 8:18 pm

Re: Reading JSON Problem(s)

Post by jonboy49 »

Sorry - off the top of my head I have no clue. Will try to take another look at it later on.

Just out of interest, what happens if you just call YAJLINTO from the command line? Can the system find it then?
jonboy49
Posts: 207
Joined: Wed Jul 28, 2021 8:18 pm

Re: Reading JSON Problem(s)

Post by jonboy49 »

I notice that you are still specifying to copy in the YAJL header files and referencing the binding directory. Can't think why that would cause issues but ...
emhill
Posts: 43
Joined: Thu Jul 29, 2021 1:15 pm

Re: Reading JSON Problem(s)

Post by emhill »

Yeah... tried it both with and without that /include statement. Still get the error...
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: Reading JSON Problem(s)

Post by Scott Klement »

YAJLINTO is just a *PGM object that RPG is trying to call. Notice that in your %PARSER it says YAJL/YAJLINTO (uppercase with a library) -- but, in the error it says yajlinto (lowercase without a library) which makes me think you forgot to recompile the program after changing it, or perhaps you're hitting a different version of the program in your library list or something like that.
emhill
Posts: 43
Joined: Thu Jul 29, 2021 1:15 pm

Re: Reading JSON Problem(s)

Post by emhill »

I re-created YAJLINTO and I am no longer getting that error. Now I'm getting this:

The document for the DATA-INTO operation does not match the RPG variable;
reason code 5. The document contains extra names that do not match subfields.

Again here is my document:'

Code: Select all

{
  "status": "Success",
  "message": "Record Retrieved Successfully",
  "interfaceName": "I-207 - Batch Claim Submittal Response",
  "batchClaimResponse": [
    {
      "businessPartnerCode": "0322400000",
      "claimInfo": {
        "claimNumber": "M4895710",
        "trackingNumber": "M023434499",
        "repairingOutlet": "0322400006",
        "repairOrder#": "227533",
        "serialNumber": "6610509335",
        "submittalIndicator": "I",
        "dateTimeProcessed": "2020-02-05T12:00:00.000",
        "claimStatusCode": "30",
        "claimStatusDescription": "Pending Approval",
        "dispositionCode": "",
        "dispositionCodeDescription": "",
        "rmaStatusCode": "",
        "rmaStatusDescription": "",
        "approvedDollarAmount": "",
        "identifier": "12345",
        "claimProcessMsgInfo": [
          {
            "lineNumber": "1",
            "processCode": "1371",
            "processCategoryDescription": "CLAIM",
            "processMessage": "AT: Travel requires manual review."
          },
          {
            "lineNumber": "2",
            "processCode": "1403",
            "processCategoryDescription": "NET ITEMS",
            "processMessage": "AT: Net Item exceeds auto-approval limit in table"
          }
        ]
      }
    }
  ]
}


And here is my modified RPG:

Code: Select all

**free                                                    
 ctl-opt dftactgrp(*no) debug option(*srcstmt:*nodebugio);
 ctl-opt bnddir('QC2LE':'YAJL');                          
                                                          
 dcl-ds allresponse qualified;                            
    status char(20);                                      
    message char(60);                                     
    interface char(80);                                   
    dcl-ds claimresponse dim(150);                        
       partnercode char(10);                              
          dcl-ds claiminfo;                               
             claim# char(20);                             
             track# char(10);                             
             repairout char(10);                          
             ro char(6);                                  
             serial# char(10);                            
             subind char(1);                              
             datetime char(23);                           
             statuscd char(3);                            
             statusds char(80);                           
             dispcode char(10);                           
             dispdesc char(80);                           
             rmacode  char(3);                            
                          rmadesc  char(80);                           
             apprdolr char(15);                           
             identifier char(10);                         
          end-ds;                                         
          dcl-ds process dim(300);                        
             lineno char(10);                             
             proccode char(4);                            
             procdesc char(25);                           
             procmsg  char(80);                           
          end-ds;                                         
       end-ds;                                            
    end-ds;                                               
                                                          
    dcl-s jsonpath varchar(150)                           
       inz('/eric/4WARD/Claim Submit Response Test.json');
                                                          
    data-into allresponse %data(jsonpath:                 
                          'doc=file case=any')            
                          %Parser('YAJL/YAJLINTO');       
                                                          
    *inlr = *on;                                          
I know there is something with the data structure not correct but I'm not sure what it is.
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: Reading JSON Problem(s)

Post by Scott Klement »

The names in the RPG have to match the names in the JSON.

The JSON has

Code: Select all

{
  status:
  message:
  interfaceName:
  batchClaimResponse: [
    ... more ...
}
But your RPG has:

Code: Select all

{ 
   status:  (matches)
   message:  (matches)
   interface: (does not match)
   claimResponse: (does not match)
emhill
Posts: 43
Joined: Thu Jul 29, 2021 1:15 pm

Re: Reading JSON Problem(s)

Post by emhill »

Well... that'll do it every time!!!!

Thanks Scott!!!!
Post Reply