bvstone

Building JSON Data with RPG

Posted:

Building JSON Data with RPG

I've been playing around lately with creating Android Apps with the MIT App Inventor.  A few years ago we used the App Inventor by Google (which was taken over by MIT) and thought it was fun.

I uses a "puzzle" like UI to build applications taking a lot of the complication out of it, as compared to using the Android SDK. 

The sample above shows the UI, and while fun, it does have it's shortfalls.  But we'll save that for another day.

In creating my app I found that using a DB server would make it a lot more interactive and feature filled.  So, I started writing CGI programs to output JSON data that could be retrieved with a call to a URI.  

One thing I needed to do was list users that were signed on.  So the first thing I did was create a template for the eRPG SDK that looks like the following:

/$section1
{
 
/$userlist
 "user/%count%/":"/%userid%/",
 "userdesc/%count%/":"/%desc%/"
 
/$comma
,

/$end
 "count":/%count%/
}

The template consists of 3 sections.  The first section is for the start of the JSON data.  Then for each user in the list, we will specify a field named user<count> and userdesc<count> where <count> is the index of each user.  Finally, we output the number of users in the list.

The RPG program for this looks like the following:

     H DFTACTGRP(*NO) BNDDIR('ERPGSDK') BNDDIR('QC2LE')
      ****************************************************************
      * Prototypes                                                   *
      ****************************************************************
      /COPY QCOPYSRC,P.ERPGSDK
      /COPY QCOPYSRC,P.SQL
      /COPY QCOPYSRC,P.DH
      ****************************************************************
     D USERDS        E DS                  EXTNAME(USERPF)
     D WEAPONDS      E DS                  EXTNAME(WEAPONPF) PREFIX(w_)
      *
     D inID            S                   LIKE(ID)
      *
     D i               S             10i 0
     D count           S             10i 0
     D tempDistance    S             13p 6
     D distance        S                   LIKE(w_DISTANCE)
     D myLat           S             13p 6
     D myLng           S             13p 6
      ****************************************************************
      /free
       #startup();
       #writeTemplate('stdhtmlheader.erpg');
       #loadTemplate('lstusr.erpg');
       #loadSection('section1');
       #writeSection();

       inID = #getData('id');

       exec sql
         select LAT, LONG into :myLat, :myLng from USERPF
           where ID = :inID;

       count = 0;

       exec sql
         declare C1 cursor for
           select ID, LAT, LONG from USERPF;

       exec sql
         open C1;

       exec sql
         fetch from C1
           into
             :ID, :LAT, :LONG;

       #loadSection('userlist');

       dow (xSQLState2 = Success_On_Sql);
         count += 1;

         tempDistance = #latLngDist(myLat:myLng:
                                    LAT:LONG);

         if (count > 1);
           #writeThisSec('comma');
         endif;

         #loadSection('userlist');
         #replaceData('/%count%/':%char(count));
         #replaceData('/%userid%/':ID);

         if (tempDistance < 1);
           eval(h) distance = (tempDistance * 1609.34);
           #replaceData('/%desc%/':%trim(ID) + ' (' +
                                   %char(distance) +
                                   ' meters)');
         else;
           eval(h) distance = tempDistance;
           #replaceData('/%desc%/':%trim(ID) + ' (' +
                                   %char(distance) +
                                   ' miles)');
         endif;

         #writeSection();

         exec sql
           fetch from C1
             into
               :ID, :LAT, :LONG;
       enddo;

       exec sql
         close C1;

       if (count > 0);
         #writeThisSec('comma');
       endif;

       #loadSection('end');
       #replaceData('/%count%/':%char(count));
       #writeSection();

       #cleanup();
       *INLR = *ON ;
      /end-free

Now, the list contains a user id as well as a description (which in this case we're including the distance from another user, since this is a location based application).

The JSON when returned looks like this:

{
   "user1":"bvstone",
   "userdesc1":"bvstone (.00 meters)",
   "user2":"wilmar",
   "userdesc2":"wilmar (285.20 miles)",
   "user3":"galapagos",
   "userdesc3":"galapagos (3100.04 miles)",
   "user4":"test",
   "userdesc4":"test (4.62 miles)",
   "count":4
}

Now, structurally we would have probably done this a little differently, but because the App Inventor can't really handle nested lists very well, we chose this method, and it works pretty well.

To parse the JSON we use the tools included in the App Developer.  This is what the "code" looks like:

First we get the number of items in the list.  Then, for each item, we read the name/value pairs and place them in a list so they are easier to work with in the structure of the App Inventor.  In this case, we use the list to populate a "list picker" object so that we can display a list of users to choose from.

So as you can see, tools like the eRPG SDK or CGIDEV2 are very useful for more than just web pages.  With the use of web services becoming much more prevalent, being creative can make you look like a hero!

And with the data formats changing quickly, from XML to JSON to whatever is next, we know at least the basic layout will allow us to hopefully only edit our templates in order to change the data format, or use a different format.


Last edited 09/08/2014 at 11:34:53



Latest Posts:

Update for Google WorkSpace Accounts (2024): Google Dropping Support for Update for Google WorkSpace Accounts (2024): Google Dropping Support for "Less Secure Apps" May 30th, 2022. What Does This Mean for Your IBM i Email?
Posted by January 19, 2024
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sales By State Report in QuickBooks Online Sales By State Report in QuickBooks Online
Posted by January 13, 2024
QuickBooks >> QuickBooks Online
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

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).