DATA-INTO to parse json response

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
Preethi
Posts: 11
Joined: Mon Apr 10, 2023 1:00 pm

DATA-INTO to parse json response

Post 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"
}
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-INTO to parse json response

Post 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.
Preethi
Posts: 11
Joined: Mon Apr 10, 2023 1:00 pm

Re: DATA-INTO to parse json response

Post by Preethi »

Thank you for your help! This works
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: DATA-INTO to parse json response

Post 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.
Post Reply