I was thinking about this, and it'd probably be helpful if I explained my analysis in more detail.
The reason you'd get a result like the one that simonerotax posted is because you had a program like this one:
Code: Select all
ctl-opt option(*srcstmt);
dcl-s theResult varchar(1000);
dcl-c LF x'25';
dcl-ds data qualified;
Json_res varchar(500);
end-ds;
data.Json_res = '{' + LF
+ ' "ErrorCode": "32767",' + LF
+ ' "Messages": [' + LF
+ ' {' + LF
+ ' "Message": "Material number non presente in +
anagrafica",' + LF
+ ' "FieldName": "",' + LF
+ ' "FieldCode": ""' + LF
+ ' },' + LF
+ ' {' +LF
+ ' "Message": "Material number non presente in +
anagrafica",' + LF
+ ' "FieldName": "",' + LF
+ ' "FieldCode": ""' + LF
+ ' }' + LF
+ ' ]' + LF
+ '}';
data-gen data %data(theResult) %gen('YAJLDTAGEN');
*inlr = *on;
Again, I haven't seen the original code, so I am making up a program that would give the result shown. In my example, I just hard coded the JSON data as a string literal directly in the program. It may not be that.. it may be generated by an earlier call to DATA-GEN, or SQL, or another JSON generator, or it may just be JSON code that the OP had somewhere.
The point is: There is a character string that contains ALREADY GENERATED JSON code.
The point of DATA-GEN is to generate the JSON from an RPG variable of some sort. In this case, the variable is a data-structure with one subfield called JSON_RES. But whatever the case, it is going to take an RPG variable and generate JSON For it.
Here you have data that is ALREADY JSON. So it makes no sense whatsoever to try to get DATA-GEN to generate. It's already generated! But since DATA-GEN is given a data structure that contains one subfield, JSON_RES, and JSON_RES is a character string, it is generating a JSON document that contains a data structure (object) and one field (json_res). The fact that the contents of Json_res happen to look like a JSON document does not matter to DATA-GEN... it doesn't interpret your character string and try to understand it. Whatever is in the string gets properly escaped to be a JSON string. A string generates a string, not a portion of a document.
Which brings me to the answer... in order to know what to tell simonerotax, I need to know how the original code works (post the code sample) and what the expected result will be.
If the only goal is to add { "Json_res": and } to the document, just concatenate them! That'll be a zillion times faster, simpler code, etc vs using DATA-GEN.
Code: Select all
TheResult = '{ "Json_res":' + data.Json_res + '}';
But maybe that's not the goal... if so, what is the goal? Why is it coded this way to begin with? Hard to know unless I've seen the code and can try to glean the intent.