James-S wrote: ↑Tue Mar 29, 2022 1:48 pm
In profound.js there is the function pjs.toObject(...) to convert a json object to a data structure that can be passed to an RPG program.
That's actually the
opposite of what it does. pjs.toObject() converts a strongly-typed data structure to an object. (Thus "toObject" because it converts to an object.)
James-S wrote: ↑Tue Mar 29, 2022 1:48 pm
Say I have a multi-level data structure like the one below.
Code: Select all
dcl-ds $partDetailTx qualified template;
tracking_id uns(10);
count uns(5);
dcl-ds detailDS dim(1600);
id uns(10);
quantity zoned(9:2);
end-ds;
end-ds;
What would the pjs.define object definition look like?
I wouldn't declare a data structure like this in Node.js unless absolutely necessary because you need to call an RPG program that accepts it as a parameter. If you just need it to create a JSON object, it'd be much more elegant to use native JavaScript methods rather than a strongly-typed data structure.
So.. assuming you wanted to call an RPG with this, you'd code it this way:
Code: Select all
pjs.define("detailDS_t", { type: 'data structure', qualified: true, template: true, elements: {
"id": { type: 'unsigned integer', length: 10, decimals: 0 },
"quantity": { type: 'decimal', length: 9, decimals: 2 }
}});
pjs.define("partDetailTx", { type: 'data structure', qualified: true, elements: {
"tracking_id": { type: 'unsigned integer', length: 10, decimals: 0 },
"count": { type: 'unsigned integer', length: 5, decimals: 0 },
"detailDS": { type: 'data structure', likeDS: 'detailDS_t', dim: 1600 }
}});
pjs.call("the-rpg-program", partDetailTx);
var obj = pjs.toObject(partDetailTx);
James-S wrote: ↑Tue Mar 29, 2022 1:48 pm
Code: Select all
{
"partDetailTx": {
"tracking_id": 12345,
"count": 2,
"detailDS": [
{
"id" : 1,
"quantity": 12.34
},
{
"id" : 2,
"quantity": 23.45
}
]
}
}
Hmmm.. this doesn't match the RPG definition that you gave!!
Every time you have a { in JSON, that must match a DCL-DS in RPG. Every time you have a } in JSON, it matches an END-DS;
So when your docu
ment starts like this:
That's not just one data structure named partDetailTx!! That's two. So the RPG definition would actually look like this (with comments showing how it matches up to the JSON:
Code: Select all
// matches this JSON object:
dcl-ds myDS qualified; // {
dcl-ds partDetailTx; // "partDetailTx": {
tracking_id uns(10); // "tracking_id": number,
count uns(5); // "count": number,
dcl-ds detailDS dim(1600); // "detailDS": [{
id uns(10); // "id": number,
quantity zoned(9:2); // "quantity": number
end-ds; // }]
end-ds; // }
end-ds; // }
That extra level where you have { "partDetailTx": { really doesn't do anything useful, though... so I don't understand why it's there. I guess you feel that it's important to see the word "partDetailTx" in the docu
ment? Since the outermost level of a docu
ment never has a name you have to add this extra level to be able to put the name in there.
The reason the outermost part of a JSON docu
ment never has a name is because you're going to assign it to a JavaScript variable. JavaScript variables have their own name, so you end up with an extra, pointless, name.
For example, imagine assigning your JSON example to a JavaScript variable:
Code: Select all
var partDetailTx = {
"partDetailTx": {
"tracking_id": 12345,
... etc ...
}
}
if (partDetailTx.partDetailTx.tracking_id == 12345) {
//code
}
Yuck! Why add that extra level? I strongly discourage you from doing that... instead, do what you did in your original RPG program where you didn't have "partDetailTx" in the JSON, but rather used it as the outermost (unnamed) element.
Code: Select all
var partDetailTx = {
"tracking_id": 12345,
}
if (partDetailTx.tracking_id == 12345) {
//code
}
Sorry... that was a detour from your question... but I wanted to make sure you understood that having the "partDetailTx": { in the docu
ment wasn't a good idea.
Oh... also your RPG program had a dollar-sign ($) character in the name of the DS ($partDetailTx). I also removed it from my JSON version because working with variables that begin with a dollar-sign in JavaScript is possible, but very ugly. I strongly recommend not using them. (I also will never use them in my RPG code since the source code wouldn't work in other countries, for example, the dollar sign would cause problems in Germany or the UK -- using $, # or @ to start a variable name in RPG is a very bad practice.)
Ok... you get the idea. I'll get off of the soap box now.