pjs.toObject()



This API returns a JavaScript object or an array of JavaScript objects. The input data structure subfield names and values are converted to JavaScript object properties so that they can be easily manipulated in JavaScript code. For example, they can be converted into JSON data to be returned from a web service.



This API was added in Profound.js 4.11.0.

See Creating a Web Service for more information on how to create a web service in Profound.js.



Parameter

  1. Data structure or data structure array.

  2. Number of elements of data structure array to process (optional; available in Profound.js release 5.3.0).

Return value

A JavaScript object (with subfield name/value properties) if input is a data structure, or an array of JavaScript objects (with subfield name/value properties) if input is a data structure array.

Example 1

Suppose you have a current RPG program GETCUST1 that returns a data structure as an output parameter, as shown below. You can easily call that RPG program and return the data as a web service, using the Profound.js code below.

Current RPG code:

D custDs1 ds qualified D custId 5p 0 D custName 20a C *entry plist C parm custDs1 /free custDs1.custId = 1; custDs1.custName = 'Customer 1 '; *inLR = *on; return; /end-free



Profound.js code as a web service to call current RPG program GETCUST1 and return data as JSON:

function GETCUST1(request, response) {  pjs.define( "custDs1", {type: "data structure", qualified: true, elements: { "custId": {type: "packed", length: 5, decimals: 0}, "custName": {type: "char" , length: 20 } }}); pjs.call("GETCUST1", custDs1); // get output parm "custDs1" var returnData = pjs.toObject(custDs1); // convert data in "custDs1" into JS objects in "returnData" response.json(returnData); // "returnData" is converted to JSON and sent back as response } exports.run = GETCUST1;



Returned data as JSON:

{ "custId": 1, "custName": "Customer 1 " }



Example 2

Suppose you have a current RPG program GETCUST2 that returns a data structure array as an output parameter, as shown below. You can easily call that RPG program and return the data as a web service, using the Profound.js code below.

Current RPG code:



Profound.js code as a web service to call current RPG program GETCUST2 and return data as JSON:



Returned data as JSON:



Example 3

Suppose you have a current RPG program GETCUST3 that returns a data structure array "custDs3" as an output parameter, plus another parameter "nbrElm" that indicates how elements of "custDs3" have actual data. You can easily call that RPG program and return the data as a web service, using the Profound.js code below.

Current RPG code:



Profound.js code as a web service to call current RPG program GETCUST3 and return data as JSON. Note that even thought "custDs3" is defined with 1000 elements, since "nbrElm" is 5, only 5 elements of array "custDs3" are processed; this could improve performance significantly when there are many elements that do not have actual data.



Returned data as JSON: