ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Incorrect JSON Message Generating (\ Occuring in the JSON )

Post new topic  Reply to topic
 Incorrect JSON Message Generating (\ Occuring in the JSON ) « View previous topic :: View next topic » 
Author Message
apmohan
PostPosted: Tue Jan 29, 2019 6:07 am    Post subject: Incorrect JSON Message Generating (\ Occuring in the JSON ) Reply with quote

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
View user's profile Send private message Send e-mail
mpong
PostPosted: Tue Jan 29, 2019 9:49 am    Post subject: Reply with quote

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
View user's profile Send private message
rekarm01
PostPosted: Tue Jan 29, 2019 11:32 am    Post subject: Re: Incorrect JSON Message Generating (\ Occuring in the JSO Reply with quote

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
View user's profile Send private message
timber
PostPosted: Tue Jan 29, 2019 1:09 pm    Post subject: Reply with quote

Grand Master

Joined: 25 Aug 2015
Posts: 1280

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
View user's profile Send private message
apmohan
PostPosted: Wed Jan 30, 2019 6:41 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
mpong
PostPosted: Thu Jan 31, 2019 11:51 am    Post subject: Reply with quote

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
View user's profile Send private message
apmohan
PostPosted: Fri Feb 01, 2019 12:54 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
martinb
PostPosted: Fri Feb 01, 2019 5:08 am    Post subject: Reply with quote

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
View user's profile Send private message
apmohan
PostPosted: Fri Feb 01, 2019 5:51 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Incorrect JSON Message Generating (\ Occuring in the JSON )
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.