bvstone

Parsing JSON using RPG with Scott Klement's Port of YAJL (Yet Another JSON Library) - Part 2

Posted:

Parsing JSON using RPG with Scott Klement's Port of YAJL (Yet Another JSON Library) - Part 2

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:

  1. Find the beginning of the JSON document (docNode)
  2. Find the beginning of the customer array (custList)
  3. For each customer in the list, retrieve the data items (email, last name, first name, etc) using the pointer to the current customer item (node)
  4. For each customer, find the beginning of the item detail list (detList)
    1. For each detail item retrieve the data items (software name, invoice date, quantity) using the pointer to the current detail item (node)
    2. Repeat previous option for each detail item
  5. For each customer, find the beginning of the license key list (keyList)
    1. For each license key item retrieve the data items (software name, logical serial number, model number, model, etc) using the pionter to the current detail item (node)
    2. Repeat previous option for each license key item
  6. Repeat items 3-5 for each customer item.

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:


Last edited 07/12/2019 at 10:53:22



Latest Posts:

How to Whitelist GreenTools for G Suite (G4G) For Your Organization How to Whitelist GreenTools for G Suite (G4G) For Your Organization
Posted by November 5, 2023
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
QuickBooks Online Releases QuickBooks Online Releases "New Invoices!"... and It's Terrible!
Posted by October 8, 2023
QuickBooks >> QuickBooks Online
Admin/4i - What is it? Admin/4i - What is it?
Posted by September 30, 2023
Vendor Corner >> MSD Information Technology
BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command
Posted by August 28, 2023
BVSTools >> BVSTools Announcements
MAILTOOL Now Allows Email Redirection for Development and Testing MAILTOOL Now Allows Email Redirection for Development and Testing
Posted by May 27, 2023
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email
Posted by March 29, 2023
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
QuickBooks Online - Subtotals and Discounts Frustration QuickBooks Online - Subtotals and Discounts Frustration
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
BVSTools Software Verified on V7R5 and Power10 BVSTools Software Verified on V7R5 and Power10
Posted by December 9, 2022
BVSTools >> BVSTools Announcements
Microsoft Office 365 Servers and Random Errors Issue Microsoft Office 365 Servers and Random Errors Issue
Posted by November 14, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending/Resending Emails Using a MIME File with MAILTOOL Sending/Resending Emails Using a MIME File with MAILTOOL
Posted by November 8, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending an HTML Email on Your IBM i Using MAILTOOL Sending an HTML Email on Your IBM i Using MAILTOOL
Posted by November 1, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Transferring License Keys from One System to Another Transferring License Keys from One System to Another
Posted by October 31, 2022
BVSTools >> BVSTools Software Discussion
Calculating the Size of a File Before Base64 Encoding Calculating the Size of a File Before Base64 Encoding
Posted by August 13, 2022
Programming >> RPG Programming
GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File
Posted by August 11, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements

Reply




© Copyright 1983-2020 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).