Re: DATA-INTO. missing nodes name
Posted: Thu Sep 01, 2022 3:26 am
Here's an example of reading the document using the subprocedures... of course, this assumes that my corrections to your format are correct -- if not it may need to be adjusted, but hopefully it gives you the idea of how the "Carrier 1", "Carrier 2", etc can be used as dynamic variable data rather than using hard-coded names like you would in data-into:
Code: Select all
**free
ctl-opt dftactgrp(*no) actgrp('KLEMENT') bnddir('YAJL');
/copy YAJL_H
dcl-ds CarrierList qualified;
num_carrier int(10) inz(0);
dcl-ds carrier dim(999);
name varchar(30);
id varchar(6);
service varchar(15);
end-ds;
validationErrors varchar(500);
end-ds;
dcl-s docNode like(YAJL_VAL);
dcl-s errMsg varchar(500);
dcl-s carriersNode like(YAJL_VAL);
dcl-s i int(10);
dcl-s count int(10);
dcl-s key varchar(50);
dcl-s val like(YAJL_VAL);
dcl-s msg char(52);
// ----------------------------------------------------------
// Load the JSON document from the IFS
// ----------------------------------------------------------
docNode = yajl_stmf_load_tree('pargent70.json': errMsg);
if docNode = *null;
// TODO: handle error
endif;
// ----------------------------------------------------------
// Find the "Carriers" node in the object
// ----------------------------------------------------------
carriersNode = yajl_object_find(docNode: 'Carriers');
if carriersNode = *null;
// Error: There was no "Carriers" node in the object.
// TODO: Handle error
endif;
// ----------------------------------------------------------
// Load the list of carriers from the "Carriers" object
// into our data structure.
// ----------------------------------------------------------
i = 0;
count = 0;
dow YAJL_OBJECT_LOOP(carriersNode: i: key: val);
count += 1;
carrierList.num_carrier = count;
CarrierList.carrier(count).name = key;
CarrierList.carrier(count).id = grabString(val: 'CarrierID');
CarrierList.carrier(count).service = grabString(val: 'CarrierService');
enddo;
// ----------------------------------------------------------
// Read the validationErrors field
// ----------------------------------------------------------
carrierList.validationErrors = grabString(docNode: 'ValidationErrors');
// ----------------------------------------------------------
// Free up YAJL resources now that everying has been
// loaded into the DS
// ----------------------------------------------------------
yajl_tree_free(docNode);
// ----------------------------------------------------------
// TODO: This is just to prove that the concept works.
// Replace with "real" business logic in the
// finished program.
// ----------------------------------------------------------
for i = 1 to carrierList.num_carrier;
msg = carrierList.carrier(i).name
+ ' id=' + carrierList.carrier(i).id
+ ' srv=' + carrierList.carrier(i).service;
dsply msg;
endfor;
*inlr = *on;
return;
// ----------------------------------------------------------
// grabString(): Returns a string node from within an object
// or '' if the node wasn't found
//
// obj = (input) object to locate the key in
// keyName = (input) key ("field name") of the string
// within the json object ("obj")
//
// Returns the value of the string, or '' if the string
// was not found or wasn't a string.
// ----------------------------------------------------------
dcl-proc grabString;
dcl-pi *n varchar(65535);
obj like(YAJL_VAL) value;
keyName varchar(50) const;
end-pi;
dcl-s result varchar(65535) static;
dcl-s tempNode like(YAJL_VAL) static;
tempNode = yajl_object_find(obj: keyName);
if tempNode = *null;
result = '';
else;
result = yajl_get_string(tempNode);
endif;
return result;
end-proc;