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
Remove unwanted \ and \n in the JSON
-
- Posts: 17
- Joined: Wed Jul 28, 2021 11:04 am
- Location: Vermont, USA
- Contact:
Re: Remove unwanted \ and \n in the JSON
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.
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.
Re: Remove unwanted \ and \n in the JSON
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
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
-
- Posts: 17
- Joined: Wed Jul 28, 2021 11:04 am
- Location: Vermont, USA
- Contact:
Re: Remove unwanted \ and \n in the JSON
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 \".
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 \".
Re: Remove unwanted \ and \n in the JSON
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
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
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: Remove unwanted \ and \n in the JSON
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()
Re: Remove unwanted \ and \n in the JSON
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
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
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: Remove unwanted \ and \n in the JSON
Are you using the current version of YAJL?