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:

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
GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function
Posted by July 24, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
What to Do If Your License Keys Don't Work What to Do If Your License Keys Don't Work
Posted by July 18, 2018
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Allow Failed Message on Invalid Recipient MAILTOOL Updated to Allow Failed Message on Invalid Recipient
Posted by May 20, 2018
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i

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