You are currently on IBM Systems Media’s archival website. Click here to view our new website.


Read, Prepare, Generate a JSON document for any DB2 File

It’s been said, “necessity is the mother of invention,” which loosely translated means “difficult situations inspire ingenious solutions.” That was the case a few years ago when I authored an article to read, prepare and generate XML for any DB2 table found on our IBM i. Now we can again use this design principle for a JSON document for any DB2 table on our system, as that as well has become a necessity.

JSON is a lightweight, text-based, human-readable format, language-independent data interchange format that is becoming increasingly popular as an alternative to XML. JSON is currently supported by many different programming language APIs as well making it a very simple to use document format.

While many applications use it for data interchange, they rarely save the JSON files to disk as the data interchange occurs between Internet-connected computers. But in our case, we wish to send data from our IBM i DB2 platform to the Internet thus creating the need to generate a JSON document to send.

This article will show just how to do that using a command, a panel group, CLLE programs and an SQL RPG program to generate a JSON document from an existing file.

Our Front-End Processing

I’ve long been an advocate for using a command interface to gather data required for a program’s execution. A command, when used with a panel group for help instructions, a validity-checking program for command input validations and program to process program for execution assures that our main program will execute with good data parameters values.

The command shown in Code Sample 1 accepts three parameters: file, document and directory.

The file parameter is the DB2 file to generate the JSON document for, document will be the name of our generated JSON document and directory is where in the IFS we’ll store the generated JSON stream file. Both the file and directory parameter are required for input and each value specified must exist on the system for execution to continue.

The panel group, shown in Code Sample 2, can be accessed by pressing the F1=Help key anywhere on the command screen and is cursor position sensitive.

A panel group developed in the user interface manager (UIM) allows a developer to provide a user with specific application help for a command, display screen, menu, etc. The development language is similar in style to HTML code. For more information on panel groups, reference the IBM Application Display Program manual (SC41-5715-02) in the IBM Information Center.

The validity-checking program allows the developer to programmatically validate entered command parameter data before initiating the program to process. If an error is found, the command screen is displayed with the status message area indicating any error(s) as shown in Code Sample 3, allowing the operator to correct the error before processing continues.

Coding for this error handling in our CLLE validity-checking program uses the IBM provided messages CPD0006 and CPF0002.

Message CPD0006 has been defined for use by the user-defined validity-checking programs. An immediate message can be sent in the message data parameter as you would with any other message you use on the system. Any diagnostic message (*DIAG) can be used for this as well if you choose not to use CPD006.

After all diagnostic messages are sent, the CPF0002 message must be sent using the SNDPGMMSG command. When the system receives message CPF0002, it sends message CPF0001 to the calling program to indicate that errors have been found. More information on the use of a Validity Checking Program can be found in the IBM CL Programming manual (SC41-5721-06) in the Information Center.

Once all input has been validated, control is passed to our program to process module (see Code Sample 4) and execution continues.

David Andruchuk is the senior architect for Computer Systems Design Associates Inc.



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters