Forums >> Programming >> RPG Programming

Jump to:




Rod

YAJL - Parsing

Posted:

YAJL - Parsing

Hello friends,

I am trying to parse below JSON using YAJL. YAJLGEN  generated below data structure but the issue i am facing is the number of arrays ex: KEY, CUSTOMER are not fixed. These arrays are returned for each field in the response. I am trying to avoid defining an array for each field from the response.

Could you please, advise if there is a better way to read the below json and parse dyanic arrays. I tried using "yajl_array_loop", "yajl_array_elem"  but  i couldn't able to make it work in my program for some reason. Thank is in advance.

 

dcl-ds jsonDoc qualified;                               
  errstatus packed(3) inz(0);                              
  dcl-ds ERRORS;                                        
    num_KEY int(10) inz(0);                  
    KEY varchar(37) inz('') dim(1);          
    num_CUSTOMER int(10) inz(0);            
    CUSTOMER varchar(43) inz('') dim(2);    
  end-ds;                                               
end-ds;                                                

***************

{
    "errstatus": 400,
     "errors": {
        "Key": [
            "The Key field is required."
        ],
        "Customer": [
            "The Customer field is required."
        ]
    }
}

 




Latest Posts:

YAJL - Parsing YAJL - Parsing
Posted by April 4, 2021
Programming >> RPG Programming
Create LPAR partition and install OS Create LPAR partition and install OS
Posted by March 24, 2021
IBM Power Systems >> (QGPL) IBM i
G4MS Now Allows User/Password Authentication Option G4MS Now Allows User/Password Authentication Option
Posted by March 16, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
IPDS printer file using CHRSIZ IPDS printer file using CHRSIZ
Posted by February 25, 2021
Programming >> RPG Programming
How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation
Posted by February 9, 2021
BVSTools >> BVSTools Software Discussion
G4MS Updated to v7.10 - Required Update for All G4MS Users G4MS Updated to v7.10 - Required Update for All G4MS Users
Posted by November 12, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
MAILTOOL Errors with Microsoft Office 365 (2020) MAILTOOL Errors with Microsoft Office 365 (2020)
Posted by September 3, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA) Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA)
Posted by August 17, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage
Posted by August 16, 2020
Programming >> Proof of Concept (POC)
GETURI v10.10 Released with New ILE Functions GETURI v10.10 Released with New ILE Functions
Posted by August 12, 2020
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
MAILTOOL Updated to Allow G4GSMAIL as Option in Routers File MAILTOOL Updated to Allow G4GSMAIL as Option in Routers File
Posted by June 28, 2020
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
BVSTools Now Offers Interface with Infor's ION APIs BVSTools Now Offers Interface with Infor's ION APIs
Posted by May 15, 2020
BVSTools >> BVSTools Announcements
More V7R4 IFS File CCSID Issues and The Fix More V7R4 IFS File CCSID Issues and The Fix
Posted by March 4, 2020
IBM Power Systems >> (QGPL) IBM i
Error Retrieving IP Address by Name Error Retrieving IP Address by Name
Posted by February 25, 2020
BVSTools >> BVSTools Software Discussion
Logging jobs that hit an outq Logging jobs that hit an outq
Posted by February 13, 2020
Programming >> CL Programming
bvstone

RE: YAJL - Parsing

Posted:

RE: YAJL - Parsing

If it were me I would use YAJL and manually spin through the array.

Something like this (it's been a while so it may not be 100%)

errorArray = YAJL_object_find(docNode:'errors');
i = 0;                                                  
dow YAJL_ARRAY_LOOP( errorArray :i :node );        
  j = 0;                                                
  dow YAJL_OBJECT_LOOP( node: j: key: val);            
    //do something here                           
  enddo;                                                
enddo;

So, when you do the 2nd look "key" will be the name of the object and val should be it's value. 

Again, you may need to play with it and dig into the YAJL docs.. .


Last edited 04/04/2021 at 08:22:20



uachieve@gmail.com

RE: RE: YAJL - Parsing

Posted:

RE: RE: YAJL - Parsing

Hi Brad, Thank you very much for your reply. I followed your suggestion to use  YAJL_ARRAY_LOOP, yajl_object_loop and it worked,


Last edited 04/10/2021 at 12:10:29




Reply




Copyright 1983-2020 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).