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:

MAILTOOL Updated to Allow G4GSMAIL as Option in Routers File MAILTOOL Updated to Allow G4GSMAIL as Option in Routers File
Posted by June 28, 2020
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
BVSTools Now Offers Interface with Infor's ION APIs BVSTools Now Offers Interface with Infor's ION APIs
Posted by May 15, 2020
BVSTools >> BVSTools Announcements
More V7R4 IFS File CCSID Issues and The Fix More V7R4 IFS File CCSID Issues and The Fix
Posted by March 4, 2020
IBM Power Systems >> (QGPL) IBM i
Error Retrieving IP Address by Name Error Retrieving IP Address by Name
Posted by February 25, 2020
BVSTools >> BVSTools Software Discussion
Logging jobs that hit an outq Logging jobs that hit an outq
Posted by February 13, 2020
Programming >> CL Programming
GreenTools for Google Apps (G4G) v12.60 Released with Shared Drive Features and More... GreenTools for Google Apps (G4G) v12.60 Released with Shared Drive Features and More...
Posted by February 4, 2020
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Allowing Requests over Port 80 For SSL Validation (ie, Namecheap, etc) Allowing Requests over Port 80 For SSL Validation (ie, Namecheap, etc)
Posted by January 31, 2020
Programming >> Web Programming
GreenTools for Slack (G4SLK) v3.00 Released GreenTools for Slack (G4SLK) v3.00 Released
Posted by January 17, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Slack (G4SLK) Specific Announcements
Calling a QSH Command from RPG Calling a QSH Command from RPG
Posted by December 26, 2019
Programming >> RPG Programming
SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages
Posted by December 14, 2019
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs
Posted by November 24, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix
Posted by November 21, 2019
IBM Power Systems >> (QGPL) IBM i
Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by November 11, 2019
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools 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).