Page 1 of 1

returning packed values via json

Posted: Mon Apr 29, 2019 1:38 pm
by trjlove
I am calling a program and want to return the results as a json object. However, those darned packed values are not playing nice. everything seems to be working, except translating the data structure to json with packed values.

if i evaluate the field value individually using the following:

Code: Select all

 console.log(logmessage + myDS[1].field1);
then the value is correct. but it blows chucks on the packed values. all other values are correct (no errors, just not readable data). I was not able to find documentation to convert the packed values.

Code: Select all

 pjs.define( 
        "myDS",   {type: "data structure", dim: 15, qualified: true, elements: {
        "field1": {type: "packed", length:  3, decimals:0, packeven: true},
        "field2": {type: "packed", length:  5, decimals:0, packeven: false},
        "field3": {type: "char",    length:  4},
        "field4": {type: "decimal", length:  3, decimals:0 }

    }});

    pjs.call("mylib/myprogram", pjs.refParm("myDS"));
    response.json({records: pjs.getDSValue(myDS)});
     

Re: returning packed values via json

Posted: Mon Apr 29, 2019 1:57 pm
by Scott Klement
I don't think you meant to use pjs.getDSValue, here. This creates a single buffer containing the DS. Basically, it's the equivalent of RPG's ability to view the entire data structure as a single string, so you get the raw buffer behind it.

Re: returning packed values via json

Posted: Mon Apr 29, 2019 3:56 pm
by Scott Klement
It'd be nice if we had a routine to convert the strict-data-type DS into a JSON primitive DS. We may add something like that in the future.

In the meantime, assuming you want the json to be the same format as your "myDS", you'd need to do something like this (after the pjs.call)

Code: Select all

var arr = [];
for (var i = 1; i <= myDs.length; i++)
  arr.push({ field1: myDs[i].field1, field2: myDs[i].field2 });
res.json(arr);
Basically, you're just constructing regular JavaScript primitive array with primitive objects inside of it, and returning that.