|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Incorrect JSON Message Generating (\ Occuring in the JSON ) |
« View previous topic :: View next topic » |
Author |
Message
|
apmohan |
Posted: Tue Jan 29, 2019 6:07 am Post subject: Incorrect JSON Message Generating (\ Occuring in the JSON ) |
|
|
Apprentice
Joined: 28 Dec 2012 Posts: 27
|
Hello,
We have a requirement to concatenate the JSON Request as Array and hit the API Gateway, but whenever we generate the message we are getting a \ in the JSON Message.
Quote: |
Req Message : <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="http://test/" xmlns:head="http://test1">
<soapenv:Header/>
<soapenv:Body>
<sen:SendUserInfo>
<head:requestHeader/>
<sen:sendUserInfoReq>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
</sen:sendUserInfoReq>
</sen:SendUserInfo>
</soapenv:Body>
</soapenv:Envelope>
|
After Transformation I am getting it as
Quote: |
["{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}},{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}},{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}"] |
Because of this we are getting failure response from API Gateway, but when we validate this it is a Valid JSON Request.
Can someone help to remove \ Please  |
|
Back to top |
|
 |
mpong |
Posted: Tue Jan 29, 2019 9:49 am Post subject: |
|
|
Disciple
Joined: 22 Jan 2010 Posts: 164
|
how are you generating your JSON array? Can you show the code snippet?
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueA'; |
|
Back to top |
|
 |
rekarm01 |
Posted: Tue Jan 29, 2019 11:32 am Post subject: Re: Incorrect JSON Message Generating (\ Occuring in the JSO |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
apmohan wrote: |
After Transformation I am getting it as
Code: |
["{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}},{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}},{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}"] |
|
The escaped double quotes represent literal characters in a larger string. So, the above JSON is just a single array element with one long string value:
Code: |
[ " ... string value ... " ] |
If the transformation should interpret the double quotes as JSON string delimiters instead, then one way to do that is to construct the JSON array as a CHARACTER string, CAST it as a BLOB, and then use the CREATE statement with PARSE clause to parse it as JSON. |
|
Back to top |
|
 |
timber |
Posted: Tue Jan 29, 2019 1:09 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
rekarm01 said:
Quote: |
one way to do that is to construct the JSON array as a CHARACTER string, CAST it as a BLOB, and then use the CREATE statement |
I wouldn't swear to it, but I'm 95% sure that you can pass a CHARACTER string directly to the parser nowadays. It will perform the conversion to BLOB internally if necessary. |
|
Back to top |
|
 |
apmohan |
Posted: Wed Jan 30, 2019 6:41 am Post subject: |
|
|
Apprentice
Joined: 28 Dec 2012 Posts: 27
|
mpong wrote: |
how are you generating your JSON array? Can you show the code snippet?
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueA'; |
I have used the same code to populate the JSON Message which is still not serving the purpose.
Code: |
DECLARE payloadCount INTEGER CARDINALITY (InputRoot.XMLNSC.*:Envelope.*:Body.*:SendUserInfo.*:sendUserInfoReq.*:Payload[]);
DECLARE concateCount INTEGER 1;
DECLARE concateValue CHARACTER '';
WHILE concateCount <= payloadCount DO
IF (concateCount = payloadCount) THEN
SET concateValue = concateValue || FIELDVALUE(InputRoot.XMLNSC.*:Envelope.*:Body.*:SendUserInfo.*:sendUserInfoReq.*:Payload[concateCount]);
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE concateValue;
ELSE
SET concateValue = concateValue || FIELDVALUE(InputRoot.XMLNSC.*:Envelope.*:Body.*:SendUserInfo.*:sendUserInfoReq.*:Payload[concateCount]) || ',';
END IF;
SET concateCount = concateCount + 1;
END WHILE;
|
|
|
Back to top |
|
 |
mpong |
Posted: Thu Jan 31, 2019 11:51 am Post subject: |
|
|
Disciple
Joined: 22 Jan 2010 Posts: 164
|
it is because escaped double quotes represent literal characters. Did you give try
Quote: |
construct the JSON array as a CHARACTER string, CAST it as a BLOB, and then use the CREATE statement with PARSE clause to parse it as JSON |
Can you post your input xml data (payload array) |
|
Back to top |
|
 |
apmohan |
Posted: Fri Feb 01, 2019 12:54 am Post subject: |
|
|
Apprentice
Joined: 28 Dec 2012 Posts: 27
|
mpong wrote: |
it is because escaped double quotes represent literal characters. Did you give try
Quote: |
construct the JSON array as a CHARACTER string, CAST it as a BLOB, and then use the CREATE statement with PARSE clause to parse it as JSON |
Can you post your input xml data (payload array) |
Please find the input request xml
Quote: |
Req Message : <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="http://test/" xmlns:head="http://test1">
<soapenv:Header/>
<soapenv:Body>
<sen:SendUserInfo>
<head:requestHeader/>
<sen:sendUserInfoReq>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
<sen:Payload>{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}</sen:Payload>
</sen:sendUserInfoReq>
</sen:SendUserInfo>
</soapenv:Body>
</soapenv:Envelope> |
|
|
Back to top |
|
 |
martinb |
Posted: Fri Feb 01, 2019 5:08 am Post subject: |
|
|
Master
Joined: 09 Nov 2006 Posts: 210 Location: UK
|
I think you've misunderstood things a bit here.
When you do
Code: |
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE concateValue; |
You are putting the whole of the JSON formatted text you extracted from your XML input into the "value" of the JSON array element, hence IIB is correctly saying hey if you need this text to be part of a JSON value, all the JSON control characters need to be escaped.
As I see it you have two options
(1) Take a very simplistic solution and just produce a BLOB output message with the content set as a contraction of "[" + your exacted concateValue + "]".
(2) A more flexible approach would be that you
- Build a temp JSON tree, in local environment etc, by parsing the your exacted concateValue, using "asbitstream"
- Then create your new JSON array and copy in the JSON objects from your temp JSON tree
HTH |
|
Back to top |
|
 |
apmohan |
Posted: Fri Feb 01, 2019 5:51 am Post subject: |
|
|
Apprentice
Joined: 28 Dec 2012 Posts: 27
|
Quote: |
(1) Take a very simplistic solution and just produce a BLOB output message with the content set as a contraction of "[" + your exacted concateValue + "]". |
This has resolved the issue and I am able to hit the request as JSON and getting a valid response from the API.
Code: |
WHILE concateCount <= payloadCount DO
IF (concateCount = payloadCount) THEN
SET concateValue = '['|| concateValue || FIELDVALUE(InputRoot.XMLNSC.*:Envelope.*:Body.*:SendUserInfo.*:sendUserInfoReq.*:Payload[concateCount])||']';
DECLARE wholeMsgBlob BLOB CAST(concateValue AS BLOB CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding );
CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(wholeMsgBlob);
ELSE
SET concateValue = concateValue || FIELDVALUE(InputRoot.XMLNSC.*:Envelope.*:Body.*:SendUserInfo.*:sendUserInfoReq.*:Payload[concateCount]) || ',';
END IF;
SET concateCount = concateCount + 1;
END WHILE;
|
Thanks a Lot for everyone for resolving this issue  |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|