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"
}
DATA-INTO to parse json response
Re: DATA-INTO to parse json response
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:
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.
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.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
Thank you for your help! This works
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: DATA-INTO to parse json response
Please use code tags.
Please don't ever post "I get an error" unless that is followed by what the error actually says.
Please don't ever post "I get an error" unless that is followed by what the error actually says.