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 IndexWebSphere Message Broker SupportConvert XML to JSON

Post new topicReply to topic Goto page 1, 2  Next
Convert XML to JSON View previous topic :: View next topic
Author Message
Connar
PostPosted: Tue Apr 03, 2012 6:26 am Post subject: Convert XML to JSON Reply with quote

Newbie

Joined: 16 Jan 2012
Posts: 6
Location: Germany

Hi all,

I am currently working on an interface where an XML result set should be converted to JSON using the JSON domain as the output domain. Until now I have written my own XML to JSON convertion code in ESQL which works pretty good. However, there may be a better (built-in) solution for this that I have not found on the internet.

Is there any possibility to let WMB automatically convert XML to JSON?


Thanks in advance.
Back to top
View user's profile Send private message
lancelotlinc
PostPosted: Tue Apr 03, 2012 7:08 am Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

automatically ?? No.
_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
Connar
PostPosted: Tue Apr 03, 2012 7:24 am Post subject: Reply with quote

Newbie

Joined: 16 Jan 2012
Posts: 6
Location: Germany

lancelotlinc wrote:
automatically ?? No.


Seems like you have a question concerning the word automatically. In that case I meant s.th. like adding a node which handles that functionality.
I know that it is possible using a XSL 2 file but as WMB does not support v2 it is not possible to use that one.
Back to top
View user's profile Send private message
lancelotlinc
PostPosted: Tue Apr 03, 2012 7:28 am Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

The business value provided by WMB is two-fold: (1) content-based routing and (2) data transformation. To convert from XML to JSON, you can use the JSON parser.

http://publib.boulder.ibm.com/infocenter/wmbhelp/v7r0m0/index.jsp?topic=%2Fcom.ibm.etools.mft.doc%2Fbc28400_.htm

Have you taken the Message Broker classroom training?

http://www-304.ibm.com/jct03001c/services/learning/ites.wss/us/en?pageType=course_description&courseCode=WM663
_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
mqjeff
PostPosted: Tue Apr 03, 2012 7:38 am Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17448

Message Broker has a JSON Parser, in newer versions.

You can use the XML parser in Message Broker - that is, the XMLNSC domain - to parse an XML document into a logical message tree.

You can then use ESQL to map the logical message tree into one that exists under the JSON parser.

Then Broker will use the JSON parser to serialize the logical message tree into a JSON structure.
Back to top
View user's profile Send private message
Connar
PostPosted: Wed Apr 04, 2012 3:35 am Post subject: Reply with quote

Newbie

Joined: 16 Jan 2012
Posts: 6
Location: Germany

Okay, I think I have not clarified my question good enough.

As I stated in my first post I am already able to convert the XML message manually to a JSON message: that means my input domain is XMLNSC, do convertion stuff in a compute node and get the output domain JSON. So far so good.

My question wasn't about how to parse the XML message nor how to parse a JSON message but whether there is a possibility to automatically convert an input domain of XML(NSC) to an output domain of JSON (like I did in the compute node) that takes care of converting multiple XML tags with the same name to JSON arrays and so on.

I hope, I was able to point out my concern better than before.

P.S.: It's okay if there is no simpler solution than using ESQL and a manual convertion. I just wondered whether there is s.th. like that already integrated to the WMB.
Back to top
View user's profile Send private message
kimbert
PostPosted: Wed Apr 04, 2012 4:50 am Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

So you're looking for a sample or a set of ESQL library procedures that do the conversion XMLNSC to JSON tree structure conversion? I don't know of any such resources, but I agree that they would be useful.
Back to top
View user's profile Send private message
mapa
PostPosted: Wed Apr 04, 2012 2:08 pm Post subject: Reply with quote

Master

Joined: 09 Aug 2001
Posts: 257
Location: Malm, Sweden

Well, not automatically but close enough?

Code:

SET OutputRoot.JSON.Data = InputBody;
Back to top
View user's profile Send private message Visit poster's website
Connar
PostPosted: Mon Apr 09, 2012 11:56 pm Post subject: Reply with quote

Newbie

Joined: 16 Jan 2012
Posts: 6
Location: Germany

mapa wrote:
Well, not automatically but close enough?

Code:

SET OutputRoot.JSON.Data = InputBody;


This is possible but not correct in the following case as an (JSON) array would be required to solve this:

Code:
<a>
  <b>1</b>
  <b>2</b>
</a>

result: {"a": {"b": 1, "b": 2}}
should be: {"a": {"b": [1, 2]}}
[/code]
Back to top
View user's profile Send private message
mapa
PostPosted: Tue Apr 10, 2012 12:30 am Post subject: Reply with quote

Master

Joined: 09 Aug 2001
Posts: 257
Location: Malm, Sweden

OK, I haven't worked with json in the broker, but thought it was worth a shot.

I guess you mean something like http://json-lib.sourceforge.net/ then.

It will be in the next Apache Camel release 2.10 via the new camel-xmljson to directly go from xml to json.
Back to top
View user's profile Send private message Visit poster's website
naio
PostPosted: Wed Feb 13, 2013 10:15 am Post subject: Reply with quote

Voyager

Joined: 08 Nov 2012
Posts: 82

Connar,

Have you get to solve this?
I am having the same problem that you:

<a>
<b>1</b>
<b>2</b>
</a>

result: {"a": {"b": 1, "b": 2}}
should be: {"a": {"b": [1, 2]}}

I am not getting the Array resolved, so the JSON fails.

Any Clue?


Thanks in Advance!
_________________
"God is in his heaven all's right with the world"
Back to top
View user's profile Send private message
kimbert
PostPosted: Thu Feb 14, 2013 2:34 am Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

In your example message, there is only one occurrence of tag <a> and two occurrences of tag <b>. When you do this:
Code:
SET OutputRoot.JSON = InputRoot.XMLNSC
...you are simply copying the syntax elements from InputRoot to OutputRoot. The XSD is not being examined, so there is no way for the copying process to know that <a> and <b> are arrays.

One solution is to explicitly mark the arrays in OutputRoot.JSON. Like this:
Code:
SET OutputRoot.JSON.root.(JSON.Array)a = InputRoot.XMLNSC.root.(XMLNSC.Element)a


I assume you have read this topic, but just in case you missed it: http://publib.boulder.ibm.com/infocenter/wmbhelp/v8r0m0/topic/com.ibm.etools.mft.doc/bc28411_.htm
Back to top
View user's profile Send private message
naio
PostPosted: Thu Feb 14, 2013 4:50 am Post subject: Reply with quote

Voyager

Joined: 08 Nov 2012
Posts: 82

Thanks kimbert,

I have read the inforcenter, But I still cannot make it work. I will give the detail of what is happening:

I have the following XML (an extract):
<traces>
<id>AAA</id>
<type>BBB</type>
<namespace>CCC</namespace>
</traces>

So if trie directly with:

SET OutputRoot.JSON.Data = Inputbody;

I get the tag <traces> as an Element and it is an Array, so i tried:

SET OutputRoot.JSON.Data.(JSON.Array)traces = ....

But that action, gives me:

{"traces":["AAA","BBB","CCC"]}

And I am Expecting:

{"traces":["id":"AAA","type":"BBB","namespace":"CCC"]}

The name of the elements are erased, I have tried with diferent combinations But I am not able to make it work.

Thanks In Advance!
_________________
"God is in his heaven all's right with the world"
Back to top
View user's profile Send private message
kimbert
PostPosted: Thu Feb 14, 2013 6:04 am Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

Try this:
- create the JSON that you want your message flow to produce
- parse it using the JSON parser in a simple message flow. Make sure that the flow contains a Trace node after the input node.
- Examine the Trace node output and modify your flow to produce exactly the same message tree
Back to top
View user's profile Send private message
naio
PostPosted: Sun Feb 17, 2013 5:54 pm Post subject: Reply with quote

Voyager

Joined: 08 Nov 2012
Posts: 82

kimbert, that is allways a good idea.
I could solved it, in this way.

I re-construct the input XML message into a message in the JSON Domain, field by field, and defining Arrays as Json.Arrays and Object and Element as them.

In that Way I could construct the final JSON Message!


Thanks a Lot!
_________________
"God is in his heaven all's right with the world"
Back to top
View user's profile Send private message
Display posts from previous:
Post new topicReply to topic Goto page 1, 2  Next Page 1 of 2

MQSeries.net Forum IndexWebSphere Message Broker SupportConvert XML to 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.