In Part 1 of this series we demonstrated how to parse a relatively simple JSON document using YAJL Port provided by Scott Klement.
The data we used was based on the data created in an article that covered creating JSON data using YAJL.
For this next example we are going to be using data that is a little more complicated and contains nested arrays. The data will look like the following:
{
"customerList": [
{
"email": "gary.anderson@company1.com",
"lname": "Anderson",
"fname": "Gary",
"comp": "Company 1, Inc.",
"add1": "123 Main Street",
"add2": "",
"city": "Nashville",
"state": "TN",
"zip": "37203",
"country": "USA",
"detail": [
{
"software": "SPLTOOL",
"invoice_date": "04/12/2007",
"qty": 1
},
{
"software": "SPLTOOL",
"invoice_date": "11/28/2011",
"qty": 1
},
{
"software": "SPLTOOL",
"invoice_date": "08/04/2015",
"qty": 1
}
],
"keys": [
{
"software": "SPLTOOL",
"l_ser": "060xxxx1",
"ser": "060xxxx",
"model": "E4B",
"key": "7HBYJB991",
"ver": "v3",
"active": false
},
{
"software": "SPLTOOL",
"l_ser": "103xxxx1",
"ser": "103xxxx",
"model": "520",
"key": "72F777JI52",
"ver": "v3",
"active": false
},
{
"software": "SPLTOOL",
"l_ser": "21Fxxxx1",
"ser": "21xxxx",
"model": "41A",
"key": "JIJ0B50II4",
"ver": "v3",
"active": true
}
]
},
{
"email": "geirpeterson@company2.no",
"lname": "Peterson",
"fname": "Geir",
"comp": "Company 2",
"add1": "Postboks 65545",
"add2": "",
"city": "",
"state": "Oslo",
"zip": "0102",
"country": "Norway",
"detail": [
{
"software": "SPLTOOL",
"invoice_date": "10/20/2004",
"qty": 1
},
{
"software": "OBJTOOL",
"invoice_date": "05/11/2006",
"qty": 1
},
{
"software": "SPLTOOL",
"invoice_date": "01/04/2011",
"qty": 2
},
{
"software": "MAILTOOL",
"invoice_date": "09/27/2013",
"qty": 2
},
{
"software": "MAILTOOLP",
"invoice_date": "09/27/2013",
"qty": 2
},
{
"software": "MAILTOOL",
"invoice_date": "08/14/2015",
"qty": 3
},
{
"software": "MAILTOOLP",
"invoice_date": "08/14/2015",
"qty": 3
},
{
"software": "SPLTOOL",
"invoice_date": "08/14/2015",
"qty": 3
}
],
"keys": [
{
"software": "SPLTOOL",
"l_ser": "",
"ser": "44Gxxxx",
"model": "820",
"key": "G07BH665I2",
"ver": "v2",
"active": false
},
{
"software": "OBJTOOL",
"l_ser": "",
"ser": "65xxxx",
"model": "825",
"key": "444II2F9I7",
"ver": "v1",
"active": false
},
{
"software": "SPLTOOL",
"l_ser": "",
"ser": "65xxxx",
"model": "825",
"key": "B033Y0I962",
"ver": "v2",
"active": false
},
{
"software": "MAILTOOL",
"l_ser": "06Bxxxx",
"ser": "06BB614",
"model": "E4A",
"key": "Q72FB675IY",
"ver": "v3",
"active": false
},
{
"software": "MAILTOOLP",
"l_ser": "06Bxxxx",
"ser": "06BB614",
"model": "E4A",
"key": "YUIH5F275JK",
"ver": "v3",
"active": false
},
{
"software": "SPLTOOL",
"l_ser": "06Bxxxx",
"ser": "06BB614",
"model": "E4A",
"key": "755567YT5",
"ver": "v3",
"active": false
},
{
"software": "MAILTOOL",
"l_ser": "06Bxxxx",
"ser": "06B9B74",
"model": "E4A",
"key": "TY5H7255I6",
"ver": "v3",
"active": false
},
{
"software": "MAILTOOLP",
"l_ser": "06Bxxxx",
"ser": "06B9B74",
"model": "E4A",
"key": "668H5H5H9I2",
"ver": "v3",
"active": false
},
{
"software": "SPLTOOL",
"l_ser": "06Bxxxx",
"ser": "06B9B74",
"model": "E4A",
"key": "H6F5H2H654",
"ver": "v3",
"active": false
},
{
"software": "MAILTOOL",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "FJ57220755",
"ver": "v3",
"active": true
},
{
"software": "MAILTOOLP",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "56HH2550TY2",
"ver": "v3",
"active": true
},
{
"software": "SPLTOOL",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "7RE0BH2TR5",
"ver": "v3",
"active": true
},
{
"software": "MAILTOOL",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "FTRGFF75T2",
"ver": "v3",
"active": true
},
{
"software": "MAILTOOLP",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "5597GF9H5R4",
"ver": "v3",
"active": true
},
{
"software": "SPLTOOL",
"l_ser": "21Exxxx1",
"ser": "21Exxxx",
"model": "41A",
"key": "TIGFHIH5R2",
"ver": "v3",
"active": true
}
]
}...
]
}
As we can see, in this data not only do we have an array of customers, but each customer array element also contains an array that describes the items purchased and a second array that contains a list of the license keys for the products were purchased.
The program used to parse this data is as follows:
H DFTACTGRP(*NO) BNDDIR('YAJL')
****************************************************************
* Imports
****************************************************************
/include yajl_h
****************************************************************
* Global Definitions
****************************************************************
D CSTMSTDS E DS EXTNAME(CSTMSTPF)
D CSTDTLDS E DS EXTNAME(CSTDTLPF)
D CSTKEYDS E DS EXTNAME(CSTKEYPF)
****************************************************************
* Work Variables
****************************************************************
D docNode s like(yajl_val)
D custList s like(yajl_val)
D detList s like(yajl_val)
D keyList s like(yajl_val)
D node s like(yajl_val)
D val s like(yajl_val)
*
D i s 10i 0
D j s 10i 0
D k s 10i 0
D testDate s 26
D errMsg s 500a varying inz('')
****************************************************************
/free
docNode = yajl_stmf_load_tree( '/tmp/makejson2.json' : errMsg );
if errMsg <> '';
// handle error
endif;
exsr $Header;
yajl_tree_free(docNode);
*inlr = *on;
//***************************************************************
//* Read Header
//***************************************************************
begsr $Header;
custList = YAJL_object_find(docNode: 'customerList');
i = 0;
dow YAJL_ARRAY_LOOP(custList:i:node );
val = YAJL_object_find(node:'email');
CMEMAIL = yajl_get_string(val);
val = YAJL_object_find(node: 'lname');
CMLNAME = yajl_get_string(val);
val = YAJL_object_find(node: 'fname');
CMFNAME = yajl_get_string(val);
val = YAJL_object_find(node: 'comp');
CMFCOMP = yajl_get_string(val);
val = YAJL_object_find(node: 'add1');
CMFADD1 = yajl_get_string(val);
val = YAJL_object_find(node: 'add2');
CMFADD2 = yajl_get_string(val);
val = YAJL_object_find(node: 'city');
CMFCITY = yajl_get_string(val);
val = YAJL_object_find(node: 'state');
CMFSTATE = yajl_get_string(val);
val = YAJL_object_find(node: 'zip');
CMFZIP = yajl_get_string(val);
val = YAJL_object_find(node: 'country');
CMFCNT = yajl_get_string(val);
detList = YAJL_object_find(node:'detail');
keyList = YAJL_object_find(node:'keys');
exsr $Detail;
exsr $Keys;
enddo;
dsply i;
endsr;
//***************************************************************
//* Customer Detail
//***************************************************************
begsr $Detail;
j = 0;
dow YAJL_ARRAY_LOOP(detList:j:node );
val = YAJL_object_find(node:'software');
CDSOFT = yajl_get_string(val);
val = YAJL_object_find(node:'invoice_date');
testDate = yajl_get_string(val);
test(ez) testDate;
if (not %error);
CDINVD = %timestamp(testDate);
endif;
val = YAJL_object_find(node:'qty');
CDCREQ = yajl_get_number(val);
enddo;
endsr;
//***************************************************************
//* Customer Keys
//***************************************************************
begsr $Keys;
k = 0;
dow YAJL_ARRAY_LOOP(keyList:k:node );
val = YAJL_object_find(node:'software');
CKSOFT = yajl_get_string(val);
val = YAJL_object_find(node:'l_ser');
CKLSER = yajl_get_string(val);
val = YAJL_object_find(node:'ser');
CKSER = yajl_get_string(val);
val = YAJL_object_find(node:'model');
CKMODEL = yajl_get_string(val);
val = YAJL_object_find(node:'key');
CKKEY = yajl_get_string(val);
val = YAJL_object_find(node:'ver');
CKKVER = yajl_get_string(val);
val = YAJL_object_find(node:'active');
if (YAJL_is_true(val));
CKACT = 'Y';
else;
CKACT = 'N';
endif;
enddo;
endsr;
This program is similar to the one from Part 1 of this series. The only additions are two subroutines. One for processing the detail records ($Detail) and one for processing any license keys ($Keys).
Because we are introducing two new arrays, we also will need two additional pointer variables that are defined like our other pointers docNode, customerList, and node. The two new pointers are named detList and keyList.
For each customer object we process, before we move to the next one we take a little detour an process the two additional arrays for the detail and key lists. We get the starting points for each list using the following code from or example:
detList = YAJL_object_find(node:'detail');
keyList = YAJL_object_find(node:'keys');
As you recall, as we loop through the customer array the pointer named node is pointing at the current customer item. We are using this as a starting point in the call to YAJL_object_find() to position the pointers for the detail and key lists that are associated with each customer. Then we use the YAJL_ARRAY_LOOP() procedure to loop through each customer item. We do the same for each of the detail items and license key items as well as shown in the following code snippet:
//***************************************************************
//* Customer Detail
//***************************************************************
begsr $Detail;
j = 0;
dow YAJL_ARRAY_LOOP(detList:j:node );
val = YAJL_object_find(node:'software');
CDSOFT = yajl_get_string(val);
val = YAJL_object_find(node:'invoice_date');
testDate = yajl_get_string(val);
test(ez) testDate;
if (not %error);
CDINVD = %timestamp(testDate);
endif;
val = YAJL_object_find(node:'qty');
MONITOR;
CDCREQ = yajl_get_number(val);
ON-ERROR;
CDCREQ = 0;
ENDMON;
enddo;
endsr;
It's important to notice that the first parameter for the YAJL_ARRAY_LOOP() procedure is the pointer to the beginning of the detail array. Also, we are using a unique index for this loop (j) but the name of the third parameter remains node just as with the processing of the customer list. Think of the node variable as a temporary place holder that we process immediately. Once we get the node to process, we do so and then are done with the pointer. Each iteration of the YAJL_ARRAY_LOOP() will reset the pointer to the next JSON object that we're processing no matter which array we're processing.
If you look at the $Keys subroutine you'll see again that we use the keyList pointer as the start, we use a unique index (k in this case) and the same node pointer for the result.
The following diagram has been updated to show how the additional pointers will work:
So, the process goes something like this:
So, as we can see the most difficult thing when processing JSON with the YAJL port will be keeping track of which pointers to use and when. I've always been a big proponent of drawing things out, and in the case of processing JSON, especially as it gets more complex, this may be your best bet to make sure things are done properly.
Related Articles: