Author |
Message
|
Connar |
Posted: Tue Apr 03, 2012 6:26 am Post subject: Convert XML to JSON |
|
|
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 |
|
 |
lancelotlinc |
Posted: Tue Apr 03, 2012 7:08 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
|
Back to top |
|
 |
Connar |
Posted: Tue Apr 03, 2012 7:24 am Post subject: |
|
|
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 |
|
 |
lancelotlinc |
Posted: Tue Apr 03, 2012 7:28 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
|
Back to top |
|
 |
mqjeff |
Posted: Tue Apr 03, 2012 7:38 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
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 |
|
 |
Connar |
Posted: Wed Apr 04, 2012 3:35 am Post subject: |
|
|
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 |
|
 |
kimbert |
Posted: Wed Apr 04, 2012 4:50 am Post subject: |
|
|
 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 |
|
 |
mapa |
Posted: Wed Apr 04, 2012 2:08 pm Post subject: |
|
|
 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 |
|
 |
Connar |
Posted: Mon Apr 09, 2012 11:56 pm Post subject: |
|
|
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 |
|
 |
mapa |
Posted: Tue Apr 10, 2012 12:30 am Post subject: |
|
|
 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 |
|
 |
naio |
Posted: Wed Feb 13, 2013 10:15 am Post subject: |
|
|
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 |
|
 |
kimbert |
Posted: Thu Feb 14, 2013 2:34 am Post subject: |
|
|
 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 |
|
 |
naio |
Posted: Thu Feb 14, 2013 4:50 am Post subject: |
|
|
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 |
|
 |
kimbert |
Posted: Thu Feb 14, 2013 6:04 am Post subject: |
|
|
 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 |
|
 |
naio |
Posted: Sun Feb 17, 2013 5:54 pm Post subject: |
|
|
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 |
|
 |
|