CGI (Common Gateway Interface) refers to the method by which RPGLE programs interact with HTTP requests/responses via the IBM i HTTP server. A CGI RPGLE program:

  • Receives HTTP requests (GET, POST, etc.)
  • Reads input from stdin (like JSON payloads)
  • Writes output to stdout (like JSON responses)

CGI Handles the connection - how do we decode the data coming in?

YAJL (Yet Another JSON Library) is a powerful open-source tool for parsing JSON data in RPGLE programs on IBM i. When decoding data from a webservice, YAJL helps transform raw JSON into usable RPG variables or data structures.

How YAJL and CGI Work Together

In a typical IBM i webservice setup the RPGLE program is configured as a CGI script in the HTTP server. YAJL is used inside that RPGLE program to parse incoming JSON and generate outgoing JSON using methods like GET, POST, PUT, DELETE.

A typical Workflow for a CGI program, written in RPGLE and using YAJL might loko like this: 

  • Receive JSON Payload Your RPGLE program receives a JSON string, usually via an HTTP POST or GET request handled by the IBM i HTTP server.
  • Load JSON into Memory Use yajl_buf_loadTree() or similar APIs to load the JSON string into a YAJL tree structure.
  • Navigate the JSON Tree Traverse the tree using YAJL functions like yajl_object_find() or yajl_array_get() to locate specific keys, values, or nested objects.
  • Extract Data Convert JSON values into RPG variables using functions like yajl_get_string(), yajl_get_number(), or yajl_get_boolean().
  • Use the Data Once decoded, the data can be used in your business logic whether you're updating DB2 records, triggering workflows, or generating responses.

For example:

jsonNode = yajl_buf_loadTree(%trim(jsonString));
customerName = yajl_get_string(yajl_object_find(jsonNode: 'name'));
orderTotal = yajl_get_number(yajl_object_find(jsonNode: 'total'));

Here’s a snippet from a code example from another lesson in this class:

docNode = yajl_stdin_load_tree(*on : errMsg : payload_ifs);
...
payload_ifs='/home/nicklitten/WEB2IFSRPG-new-'+%char(%timestamp:*iso0)+'-PUT.json';

// get the JSON document loaded into memory
// **AND** save the payload direct to the IFS location
docNode=yajl_stdin_load_tree(*on:errMsg:payload_ifs);

if (docNode=*NULL) or (errMsg <> ' ');
  ds_methodPUT.success=*off;
  ds_methodPUT.errorMsg='* Fail - '+errMsg;
else;
  ds_methodPUT.success=*on;
  ds_methodPUT.errorMsg='Success. Json successfully stored at:'+
payload_ifs;
endif;

yajl_tree_free(docNode);

This shows YAJL reading the JSON payload from the HTTP POST and writing a response in classic CGI behavior, powered by YAJL.

If you're building a webservice, YAJL is your JSON Swiss Army knife, and CGI is the doorway to the web.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>