Hi Jason,
On 1/20/2020 10:57 AM, Jason Olson wrote:
We'll just use the subprocedures then.
I was looking over your document again since I had some time this
morning. It looks like the basic structure is that the first array
element is information about the other array elements... like
header-level info about the document you're parsing.
As such, I was thinking a good approach might be to do the following
(see sample code below). I am not sure about the proper sizes, etc, for
all of the fields, and I didn't take the time to code all of them, but I
think this will give you the idea of what I'd do.
Hope it helps!
**free
ctl-opt dftactgrp(*no) bnddir('YAJL');
/copy yajl_h
dcl-ds Info qualified;
Count int(10);
DataSrc varchar(100);
Oper varchar(50);
Time varchar(30);
UserId varchar(100);
end-ds;
dcl-ds Item qualified dim(50);
dcl-ds Attr;
DTE char(2);
Market char(4);
MarketName varchar(100);
RequestedSymbol varchar(10);
Status char(1);
TickerSymbol varchar(10);
Vendor char(3);
end-ds;
dcl-ds Data;
TradeDateTime varchar(30);
// ... etc ...
end-ds;
end-ds;
dcl-s docNode like(yajl_val);
dcl-s infoObj like(yajl_val);
dcl-s itemObj like(yajl_val);
dcl-s attrObj like(yajl_val);
dcl-s dataObj like(yajl_val);
dcl-s errMsg varchar(500);
dcl-s ItemNo int(10);
docNode = yajl_stmf_load_tree('jasonOlson.json': ErrMsg);
if docNode = *null or errMsg <> '';
// display errMsg, or whatever you wish to do when there's an error
*inlr = *on;
return;
endif;
// The first element of the array is an object
// containing header-level information about the
// document.
infoObj = YAJL_ARRAY_ELEM(docNode: 1);
Info.Count = %int(getObjString(infoObj: 'Count'));
Info.DataSrc = getObjString(infoObj: 'DataSrc');
Info.Oper = getObjString(infoObj: 'Oper');
Info.Time = getObjString(infoObj: 'Time');
Info.UserId = getObjString(infoObj: 'UserID');
// Array elements 2+ are the actual data items that
// the header info refers to.
for ItemNo = 1 to Info.Count;
itemObj = YAJL_ARRAY_ELEM(docNode: ItemNo+1);
AttrObj = yajl_object_find(itemObj: 'Attr');
Item(ItemNo).Attr.DTE = getObjString(AttrObj: 'DTE');
Item(ItemNo).Attr.Market = getObjString(AttrObj: 'Market');
Item(ItemNo).Attr.MarketName = getObjString(AttrObj: 'MarketName');
Item(ItemNo).Attr.RequestedSymbol = getObjString(AttrObj:
'RequestedSymbol');
Item(ItemNo).Attr.Status = getObjString(AttrObj: 'Status');
Item(ItemNo).Attr.TickerSymbol = getObjString(AttrObj:
'TickerSymbol');
Item(ItemNo).Attr.Vendor = getObjString(AttrObj: 'Vendor');
DataObj = yajl_object_find(itemObj: 'Data');
Item(ItemNo).Data.TradeDateTime = getObjString(DataObj:
'TradeDateTime');
// .. etc ...
endfor;
yajl_tree_free(docNode);
*inlr = *on;
return;
// --------------------------------------------------------------------
// getObjString(): "Shortcut" subprocedure to get a field in an
// YAJL object and interpret it as a string.
// if the field is not found, this returns an
// empty string.
// --------------------------------------------------------------------
dcl-proc getObjString;
dcl-pi *n varchar(1000);
obj like(yajl_val) value;
key varchar(100) const;
end-pi;
dcl-s node like(yajl_val);
dcl-s retval varchar(1000);
node = yajl_object_find(obj: key);
if node = *null;
retval = '';
else;
retval = yajl_get_string(node);
endif;
return retval;
end-proc;
As an Amazon Associate we earn from qualifying purchases.