Remove unwanted \ and \n in the JSON

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
litu23
Posts: 4
Joined: Mon Feb 14, 2022 3:14 pm

Remove unwanted \ and \n in the JSON

Post by litu23 »

Hi Team,

I am using YAJL to generate JSON in my RPG program. But the output string is containing information like below:
"OUT_JSON": "{\n \"APInvoices\": [\n {\n \"runID\":
What I am missing and how I can remove these additional elements (\ and \n) from my JSON.


Thanks,
Litu
brianjgarland
Posts: 14
Joined: Wed Jul 28, 2021 11:04 am
Location: Vermont, USA
Contact:

Re: Remove unwanted \ and \n in the JSON

Post by brianjgarland »

The slash indicates an escape character. My guess is you added a string containing the { and what follows instead of adding those elements individually.

Can you share some code? Both how you build the JSON object and how you extract it into a string. That may help us determine what is happening.
litu23
Posts: 4
Joined: Mon Feb 14, 2022 3:14 pm

Re: Remove unwanted \ and \n in the JSON

Post by litu23 »

This is my code where I have started creating the JSON in RPGLE

Dcl-S rc Int(10);
Dcl-S len Int(10);
Dcl-S Out_Json Char(16000000);

yajl_genOpen(*ON);
yajl_beginObj();
yajl_addChar('APInvoices');
yajl_beginArray();
yajl_beginObj();
yajl_addNum('runID':%Trim(%char(Runid)));
yajl_addChar('loadGroup' : %Trim(Loadgroup));
yajl_addNum('invoice':%trim(%char(Invoice)));
yajl_endObj();
yajl_endArray();
yajl_endObj();

Then finally putting the JSON using below code in Out_Json string:
rc = yajl_copyBuf( 0
: %addr(Out_Json)
: 16000000
: len );

Let me know, if you need any additional information.

Thanks,
Litu
brianjgarland
Posts: 14
Joined: Wed Jul 28, 2021 11:04 am
Location: Vermont, USA
Contact:

Re: Remove unwanted \ and \n in the JSON

Post by brianjgarland »

On yajl_genOpen that first parameter is whether or not to make the JSON "pretty". If you pass *OFF instead of *ON it will drop all the line feeds (\n) from the string.

You don't show how you added "OUT_JSON" to the JSON object in your code example but it looks like that may be where the \" is coming from. Are you adding the output from yajl_copyBuf() as a string? That would explain the \".
litu23
Posts: 4
Joined: Mon Feb 14, 2022 3:14 pm

Re: Remove unwanted \ and \n in the JSON

Post by litu23 »

On yajl_genOpen that first parameter is whether or not to make the JSON "pretty". If you pass *OFF instead of *ON it will drop all the line feeds (\n) from the string. --- I have tried with *OFF in that scenario also I am getting these additional '\'.

You don't show how you added "OUT_JSON" to the JSON object in your code example but it looks like that may be where the \" is coming from. Are you adding the output from yajl_copyBuf() as a string? -- Yes I am adding output from yajl_copyBuf() as a string. Is that any other way we can do so that these '\' will not come. My requirement is to pass the JSON as output in the IWS.

Please let me know, if you need any additional information.

Thanks,
Litu
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: Remove unwanted \ and \n in the JSON

Post by Scott Klement »

If you have pre-formatted JSON data that you just want to insert into the middle of a document (without any syntax checking, escaping, etc) you can use yajl_addPreformattedPtr()
litu23
Posts: 4
Joined: Mon Feb 14, 2022 3:14 pm

Re: Remove unwanted \ and \n in the JSON

Post by litu23 »

Hi Scott,

As per your instruction I am trying to add yajl_addPreformattedPtr() in the code but getting compilation error like '*RNF7030 30 002600 The name or indicator YAJL_AD... is not defined. '
Please find the code below and let me know, If I am missing anything:
Ctl-Opt BndDir('YAJL/YAJL')
AlwNull(*UsrCtl)
Option(*NoDeBugIO:*SrcStmt)
PgmInfo(*Pcml:*Module);
/Include YAJL/QRPGLESRC,YAJL_H
YAJL_addPreformattedPtr(
'data'
: %addr(data: *DATA)
: %len(%trimr(data))
);

Thanks
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: Remove unwanted \ and \n in the JSON

Post by Scott Klement »

Are you using the current version of YAJL?
Post Reply