The outermost element of a JSON document does not have a name (aside
from the filename of the document itself.)
You provided this example:
{"myRequest" : {
"subfield1" : "contents of subfield1",
"subfield2" : "contents of subfield2",
"subfield3" : "contents of subfield3"
}
}
In that example, "myRequest" is NOT the name of the outermost element,
it is the name of an inner element. The name applies to what comes
AFTER the colon, not what comes before it. So "myRequest" is the name
of the data structure containing "subfield1", "subfield2" and
"subfield3". But it does NOT apply to the { and } characters at the top
level of the document... these have no name. And they MUST have no name
in order to comply with JSON.
To put that in RPG terms, you can have a data structure with no name in RPG:
dcl-ds *n;
field1 char(1);
field2 char(1);
end-ds;
You'd never say "field1 is the name of my data structure!" because the
data structure has no name. field1 is the name of a subfield inside my
data structure. RPG doesn't let you nest other data structures inside
of one that doesn't have a name... but suppose it did...
dcl-ds *n;
dcl-ds myRequest;
subfield1 char(1);
subfield2 char(1);
subfield3 char(1);
end-ds
end-ds;
This isn't possible in RPG.. .but even if it were, you'd never say
"myRequest is the name of the data structure" because the data structure
has no name... it's *n which means "null" or "no name". It contains a
subfield named myRequest, which happens to be a nested data structure.
But that DOESN'T mean the outermost element is named "myRequest".
The same is true of your JSON document. Yes, it contains an internal
field named myRequest, but that is NOT the name of the outermost structure.
In JSON it's not allowed to assign a name to the outermost structure.
It's not allowed, it's not part of the JSON spec.
If your goal is to create an unnamed data structure that contains an
internal field named myRequest (which is what you posted) then use the
solution that Jon posted of putting it inside of another structure.
dcl-ds base qualified inz;
dcl-ds myRequest;
subfield1 char(10);
subfield2 char(20);
subfield3 char(30);
end-ds;
end-ds;
In my opinion, that's silly.. it's adding another pointless layer to the
JSON document that serves no purpose.. but you can do it, and it'll
work. That said, I work day in and day out with JSON, and have for well
over a decade... The ONLY time I see this sort of weirdness where
everything is inside a subfield is when someone just swithced from XML
to JSON and hadn't really learned JSON yet.
On 4/30/2024 9:16 AM, Javier Sanchez wrote:
Hi everyone.
I've got this curious case. I had to look into Scott's documentation on
DATA-GEN, and tried to apply what's in there. But it is not working. Sure
this may be my mistake, and I would like and appreciate your comments, as
usual.
The following resembles my original code and should be helpful and easy to
understand.
I've got this DS:
dcl-ds myRequest qualified inz;
subfield1 char(10),
subfield2 char(20);
subfield3 char(30);
end-ds;
dcl-s myJsonRequest char(1024) inz;
I need DATA-GEN to put in myJsonRequest the following:
{"myRequest" : {
"subfield1" : "contents of subfield1",
"subfield2" : "contents of subfield2",
"subfield3" : "contents of subfield3"
}
}
When i invoke DATA-GEN with no options, I am getting this:
{
"subfield1" : "contents of subfield1",
"subfield2" : "contents of subfield2",
"subfield3" : "contents of subfield3"
}
I was expecting the json object to inherit the "myRequest" name.
Reading on Scott's documentation on DATA-GEN, he states that I can include
in the options for %DATA, 'name=myRequest" as I wrote above, and with that,
it would produce the desired json output, but it is not doing it.
Please, what am I doing wrong?
TIA
Javier.
As an Amazon Associate we earn from qualifying purchases.