DATA-GEN \n and \"

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
simonerotax
Posts: 3
Joined: Mon Mar 04, 2024 6:56 pm

DATA-GEN \n and \"

Post by simonerotax »

Hi at all,

I apologize if this topic has already been addressed (I searched but couldn't find it)

Using DATA-GEN I return to rest-api (IWS) a json like that (with beautify" : true):
{
"Json_res": "{\n \"ErrorCode\": \"32767\",\n \"Messages\": [\n {\n \"Message\": \"Material number non presente in anagrafica\",\n \"FieldName\": \"\",\n \"FieldCode\": \"\"\n },\n {\n \"Message\": \"Material number non presente in anagrafica\",\n \"FieldName\": \"\",\n \"FieldCode\": \"\"\n }\n ]\n}\n"
}

It seems the \n are for "new row" and \" insted of ".

I have tried to change CCSID in my job (37, 1208 ecc) without a good result.
Anyone can help me?

Tks!
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-GEN \n and \"

Post by jonboy49 »

I think this is in the "Doctor it hurts when I bang my head into the wall" category. The "beautify" setting is causing this so simply remove it.

Personally I only ever use that when writing to a file to get a pretty version to check.
simonerotax
Posts: 3
Joined: Mon Mar 04, 2024 6:56 pm

Re: DATA-GEN \n and \"

Post by simonerotax »

Also without "beautify" I get \" insted of "

:|
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-GEN \n and \"

Post by jonboy49 »

Sorry - I missed the fact that the " was wrong too. I was focussed on the /n newlines.

Wanna show us the data definitions and DATA-GEN statement?

What code page does your system use?
Last edited by jonboy49 on Wed Mar 06, 2024 4:02 pm, edited 1 time in total.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: DATA-GEN \n and \"

Post by Scott Klement »

I don't see what this has to do with code pages or beautify. This appears to be completely normal and expected data.

You are embedding a JSON document inside of another JSON document. When you do that, the JSON document has to be treated as a string -- so any special characters must be escaped. This appears to be happening correctly as expected.

If your goal isn't to embed a JSON document inside of another JSON document, then you've written your RPG code incorrectly. But, it's hard to say what you'r doing wrong since we don't know what your code looks like and we don't know what your expected result would be.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: DATA-GEN \n and \"

Post by Scott Klement »

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.
simonerotax
Posts: 3
Joined: Mon Mar 04, 2024 6:56 pm

Re: DATA-GEN \n and \"

Post by simonerotax »

Scott, thanks for your response! Very nice!

My goal is to obtain a json structured like this:

Code: Select all

{
    "ResultList": [
        {
            "ErrorCode": 32767,
            "Messages": [
                {
                    "Message": "Field CodComponent=COMP-FOO-BAR does not exists in database",
                    "FieldName": "CodComponent",
                    "FieldCode": "COMP-FOO-BAR"
                }
            ]
        },
        {
            "ErrorCode": 0,
            "Messages": [
                {
                    "Message": "Alternative UM KG for Component 00#120 is saved successfully",
                    "FieldName": "AlternativeUnitMeasureCod",
                    "FieldCode": "KG"
                }
            ]
        }
    ]
}
to return like output response from a my rest-api created on IWS. I prepared data structures to put data, but the number of array elements is predefined and I don't want to return empty elements.


I therefore thought of providing a single long "output" field as the return of the rest-api and populating it with a compact json obtained from the data-gen with YAJLDTAGEN and countprefix=num_.

I get the desired result, but if I try to consume my rest api with Post-man I see in the "output" field those characters \n (if I use beautify) and \ (in any case).

However, I am changing my technique. They informed me that I can get the same compact output as a result of the rest-api by publishing variables ending with _length in the various branches from the data structure. IWS seems to be able to interpret the situation and return only the elements of the various arrays with a value inside.

I will try this method!
Post Reply