Tip 6: How to handle variable-size array in body and response (in low-code plugin Consume REST Service)



This tip documents how to handle variable-size arrays in the "body" and "response" of a REST API, as used in the low-code plugin Consume REST Service, when the low-code module is called from an RPG program. Since RPG does NOT have variable-size arrays (not until V7R4, and even that has limitations related to arrays used as CALL parameters), some extra handling is needed to interact between RPG and low-code (which is Javascript code). In Javascript and JSON, the number of elements of an array can vary at run time. When an RPG program calls a low-code module with an array parameter that's meant to be sent to a REST API, only the ACTUAL number of elements should be sent in the JSON data, NOT the maximum number of elements as defined in the CALL interface. Similary, on return from the REST API, the RPG program needs to know how many actual elments of an array parm is actually populated by the REST API.

Below is an example.

Use the code below to create JS file pjstips_06_1.js as a web service, with POST method, with URL http://localhost:8081/run/pjstips/pjstips_06_1. Refer to here on how to do that.

pjstips_06_1.js
function app(req, res) { let nameInArray = req.body.nameInArray; let nameOutArray = []; for (let i = 0; i < nameInArray.length; i++) { let nameElem = { "firstName" : nameInArray[i].firstName, "lastName" : nameInArray[i].lastName, "fullName" : `${nameInArray[i].firstName.trim()} ${nameInArray[i].lastName.trim()}` } nameOutArray.push(nameElem); } let data = {}; data.nameOutArray = nameOutArray; res.json(data); } exports.run = app;

This REST API expects a body as JSON in the format of:

{ "nameInArray" : [ { "firstName" : "AA", "lastName" : "BB" } , { "firstName" : "CC", "lastName" : "DD" } ] }



This REST API will return a JSON object in the format of:

{ "nameOutArray" : [ { "firstName" : "AA", "lastName" : "BB", "fullName" : "AA BB" } , { "firstName" : "CC", "lastName" : "DD", "fullName" : "CC DD" } ] }



Use the code below to create a module file pjstips_06_2.module.json. Refer to here on how to do that.

pjstips_06_2.module.json



The input parameters are defined like this. The object "body" should match with the structure as expected in the "body" of the REST API. The array body.nameInArray has maxinum 10 elements, but the extra parm nameInArrayNbrElem is used on the Javascript side to set the actual number of elements, before sending the "body" to the REST API.



The ouput parameters are defined like this. The object "dataOut" should match with the structure of the JSON object as returned from the REST API. The array nameOutArray has maxinum 10 elements, but the extra parm nameOutArrayNbrElem is used on the Javascript side to pass back to RPG side the actual number of elements populated by the REST API.



The routine looks like this:



Before the REST API is called, custom code is used to set the actual number of elments of array input["body"]["nameInArray"] to "truncate" the unused elements.



When you call the module, the console log will look like below. Before the "truncating" of the array, all 10 elements of nameInArray are printed (since the RPG program has no choice but to send ALL 10 elements to the Javascript side), but with only the first 2 elements populated, with the rest of the elements filled with blanks. That might cause issue for the called REST API. We should send ONLY the actual elements populated, by setting the "length" property of the array nameInArray. After the "truncating", the array nameInArray has only 2 elements as shown.



The plugin Consume REST Service is used like this:



After the response JSON is returned from the REST API, custom code is used to set the extra parm nameOutArrayNbrElem, to pass back to RPG program, to indicate the actual number of elements populated by the REST API.



If you click on the </> button ("Switch routine to Node.js code"), you'll see the "real" PJS code below, to see how it works.



Here's the RPG code to call the low-code module to test it out.

PJSTIPS062.rpgle