Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DATA-INTO - JSON MultiObject with Arrays
-----------------------------------------------------------------------------------------------------------------------------------
Thist Gist is part of iJSON-FAQ-007: Data-Into-Multi-Object-Arrays (https://blog.faq400.com/it/programmazione/json-ibmi-faq-howto/)
-----------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
JSON Example with Multi Objects and Arrays
-------------------------------------------
{
"success": true,
"header": null,
"data": {
"Messages": null,
"PostVoidReason": null,
"SuspendedOrderId": null,
"Invoice": [
{
"CreatedTimestamp": "2019-12-04T22:30:32.766",
"InvoiceId": "36407544440622146349179",
"FulfillmentDate": null,
"SellingLocationId": "0001",
"UpdatedBy": "admin@IBM.com",
"AmountProcessed": -89.25,
"TotalCharges": 0.00,
"InvoiceSubTotal": -85.00,
"ParentOrderId": "011123190031001020504",
"TaxExemptId": null
},
{
"CreatedTimestamp": "2019-12-04T22:30:32.766",
"InvoiceId": "36407544440622146349180",
"FulfillmentDate": null,
"SellingLocationId": "0001",
"UpdatedBy": "admin@IBM.com",
"AmountProcessed": -189.25,
"TotalCharges": 0.00,
"InvoiceSubTotal": -185.00,
"ParentOrderId": "011123190031001020504",
"TaxExemptId": null
}
],
"TotalTaxes": -4.25,
"BalanceDue": 0.00,
"PK": "5754986290376591116"
},
"requestUri": null,
"statusCode": "OK"
}
----------------------------------------------------------------------
XDATAINTO2: RPGLE Program
Parsing a JSON with an array within ogject within object
----------------------------------------------------------------------
ctl-opt option(*srcstmt: *nodebugio);
ctl-opt dftactgrp(*no) actgrp(*new) bnddir('HTTPAPI');
ctl-opt CCSID(*CHAR:*JOBRUN);
//------------------------------------------------
// DATA-INTO Example with Different Objets
// and Arrays in JSON document
//------------------------------------------------
dcl-pr ExcCmd ExtPgm('QCMDEXC');
Cmd_str char(1000) options(*varsize) const ;
Cmd_len packed(15:5) const ;
END-PR;
dcl-s Apostr char(1) inz(X'7D');
dcl-s Cmd_str char(512);
dcl-ds pgmStat psds;
numElements int(20) pos(372);
end-ds;
dcl-ds Invoice qualified dim(20);
CreatedTimestamp varchar(26);
InvoiceId varchar(50);
FulfillmentDate varchar(26);
SellingLocationId varchar(50);
UpdatedBy varchar(50);
AmountProcessed packed(10:2);
TotalCharges packed(10:2);
InvoiceSubTotal packed(10:2);
ParentOrderId varchar(50);
TaxExemptId varchar(50);
End-ds;
dcl-s RPGOPTS varchar(256);
dcl-s YAJLOPTS varchar(256);
dcl-s ifsPathName varchar(256);
dcl-s string char(30);
dcl-s reply char(10);
dcl-s i packed(3:0);
// DATA-INTO Options;
RPGOPTS=' path=json/data/Invoice'
+' doc=file'
+' case=any'
+' allowextra=yes'
+' allowmissing=yes';
YAJLOPTS='{"value_null":"0" '
+', "document_name": "json" '
+'}';
ifsPathName='/home/faq400/Json_example_with_Objects_and_Arrays.json';
// Per sicurezza forzo UTF8 il file
Cmd_Str = 'CHGATR OBJ(' + Apostr + %trim(ifsPathName) +
Apostr + ') ATR(*CCSID) VALUE(1208)';
ExcCmd(Cmd_Str : %Len(Cmd_Str) );
// Setto il CCSID del JOB a 280 ITA
Cmd_Str = 'CHGJOB CCSID(280)';
ExcCmd(%Trim(Cmd_Str) :
%Len(%Trim(Cmd_Str)));
Clear Invoice;
// Parsing JSON
data-into Invoice %DATA( ifsPathname : RPGOPTS)
%PARSER( 'YAJL/YAJLINTO' : YAJLOPTS);
for i=1 to numElements;
string='InvoiceId(' +%editc(i:'Z')+ '): '
+invoice(i).InvoiceId;
dsply string;
string='Amount(' +%editc(i:'Z') + ') : '
+%editc(invoice(i).AmountProcessed:'K');
dsply string;
endfor;
Dsply ( 'Press <Enter> to end program' ) ' ' reply;
// RisSetto il CCSID del JOB al default utente
Cmd_Str = 'CHGJOB CCSID(*USRPRF)';
ExcCmd(%Trim(Cmd_Str) :
%Len(%Trim(Cmd_Str)));
*InLr = *On ;
----------------------------------------------------------------------
XDATAINTO3: RPGLE Program
Parsing a JSON with an array within ogject within object
----------------------------------------------------------------------
ctl-opt option(*srcstmt: *nodebugio);
ctl-opt dftactgrp(*no) actgrp(*new) bnddir('HTTPAPI');
ctl-opt CCSID(*CHAR:*JOBRUN);
//------------------------------------------------
// XDATAINTO3 Example with Different Objets
// and Arrays in JSON document
//------------------------------------------------
dcl-pr ExcCmd ExtPgm('QCMDEXC');
Cmd_str char(1000) options(*varsize) const ;
Cmd_len packed(15:5) const ;
END-PR;
dcl-s Apostr char(1) inz(X'7D');
dcl-s Cmd_str char(512);
dcl-ds pgmStat psds;
numElements int(20) pos(372);
end-ds;
dcl-ds InvoiceTmp qualified template ;
CreatedTimestamp varchar(26);
InvoiceId varchar(50);
FulfillmentDate varchar(26);
SellingLocationId varchar(50);
UpdatedBy varchar(50);
AmountProcessed packed(10:2);
TotalCharges packed(10:2);
InvoiceSubTotal packed(10:2);
ParentOrderId varchar(50);
TaxExemptId varchar(50);
End-ds;
dcl-ds DataTmp qualified template ;
Invoice likeds(InvoiceTmp) dim(20);
TotalTaxes packed(10:2);
BalanceDue packed(10:2);
PK char(25);
End-ds;
dcl-ds MyJsonDS qualified;
success char(10);
data likeds(datatmp);
END-DS;
dcl-s RPGOPTS varchar(256);
dcl-s YAJLOPTS varchar(256);
dcl-s ifsPathName varchar(256);
dcl-s string char(30);
dcl-s reply char(10);
dcl-s i packed(3:0);
// DATA-INTO Options;
RPGOPTS=' doc=file'
+' case=any'
+' allowextra=yes'
+' allowmissing=yes';
YAJLOPTS='{"value_null":"0" '
+'}';
ifsPathName='/home/faq400/Json_example_with_Objects_and_Arrays.json';
// Per sicurezza forzo UTF8 il file
Cmd_Str = 'CHGATR OBJ(' + Apostr + %trim(ifsPathName) +
Apostr + ') ATR(*CCSID) VALUE(1208)';
ExcCmd(Cmd_Str : %Len(Cmd_Str) );
// Setto il CCSID del JOB a 280 ITA
Cmd_Str = 'CHGJOB CCSID(280)';
ExcCmd(%Trim(Cmd_Str) :
%Len(%Trim(Cmd_Str)));
Clear MyJsonds.data.Invoice;
// Parsing JSON
data-into MyJsonDS %DATA( ifsPathname : RPGOPTS)
%PARSER( 'YAJLINTO' : YAJLOPTS);
for i=1 to 2;
string='InvoiceId(' +%editc(i:'Z')+ '): '
+MyJsonds.data.invoice(i).InvoiceId;
dsply string;
string='Amount(' +%editc(i:'Z') + ') : '
+%editc(MyJsonds.data.invoice(i).AmountProcessed:'K');
dsply string;
endfor;
Dsply ( 'Press <Enter> to end program' ) ' ' reply;
// RisSetto il CCSID del JOB al default utente
Cmd_Str = 'CHGJOB CCSID(*USRPRF)';
ExcCmd(%Trim(Cmd_Str) :
%Len(%Trim(Cmd_Str)));
*InLr = *On ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.