Page 1 of 1

DATA-INTO to parse json response

Posted: Fri Feb 02, 2024 9:19 am
by Preethi
Hi All,

RPG code is receiving json response after invoking a rest api. I created the below data structure and used data-into to parse the response json. Success response works fine but receiving error for failed response. Could some help me with the solution.
*
dcl-ds Data qualified;
status char(25);
errmsg char(100);
dcl-ds invoiceData dim(1);
invoice char(10);
date char(10);
name char(30);
amount char(15);
end-ds;
responseCode char(3);
end-ds;
*
DATA-INTO epmData %DATA( json_ifs_res: 'doc=file +
case=any countprefix=num_') %PARSER('YAJLINTO');
*
The json response is as follows -
Success scenario response -
{
"status": "success",
"errmsg": "",
"data": [
{
"invoice": "13889",
"date": "02/01/2024",
"name": "Paper mate clear point",
"amount": 14.80,
}
],
"responseCode": "200"
}
*
Error Scenario response -
{
"status": "failed",
"errmsg": "Error Message Here",
"data": [ ],
"responseCode": "200"
}

Re: DATA-INTO to parse json response

Posted: Fri Feb 02, 2024 4:29 pm
by jonboy49
At first glance the easiest way to handle this would be to use countprefix. You have specified this but have not declared the relevant variable in the DS. You need to code something like this:

Code: Select all

dcl-ds Data qualified;
   status char(25);
   errmsg char(100);
   num_InvoiceData  int(5);  // Add this definition
   dcl-ds invoiceData dim(1);
      invoice char(10);
      date char(10);
      .....
P.S. Please use code tags when showing your RPG it makes it so much easier to read.
P.P.S. If you only ever get a single invoice in the response then remove the Dim from the invoiceData definirtion otherwise change the Dim to a more meaningful number.

Re: DATA-INTO to parse json response

Posted: Fri Feb 02, 2024 6:29 pm
by Preethi
Thank you for your help! This works

Re: DATA-INTO to parse json response

Posted: Fri Feb 02, 2024 6:47 pm
by Scott Klement
Please use code tags.

Please don't ever post "I get an error" unless that is followed by what the error actually says.