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:

Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by 7 hours ago
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming
Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion

Reply




Copyright 1983-2019 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).