|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
XML to JSON help with JSON Arrays needed |
« View previous topic :: View next topic » |
Author |
Message
|
marko.pitkanen |
Posted: Thu Dec 11, 2014 12:20 am Post subject: XML to JSON help with JSON Arrays needed |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
Hi,
I'm newbie with REST-like JSON interfaces and how to implement them with IIB.
I have flow in IIB with http request to the external system. I have set http request node to use xml schema from library to parse and validate XML -response from external system.
I wonder what I could do to make almost generic conversion from XMLNSC to JSON domain work properly with possible multiple times occurring elements / sub trees?
Code: |
DECLARE rStartField REFERENCE TO InputputRoot.XMLNSC.*:....;
SET OutputRoot.HTTPResponseHeader."Content-Type" = 'application/json';
SET OutputRoot.JSON.Data = rStartField;
|
Phenomenon is that if response from external system have multiple of those IIB creates JSON -arrays fine:
Code: |
"ListOfAccount": {"Account": [
{
"ParentAccountId": "xx",
"ParentAccountName": "yy",
"ParentAccountStatus": "xx",
"Id": "12313",
"AccountStatus": "YYZ",
},
{
"ParentAccountId": "xx",
"ParentAccountName": "yy",
"ParentAccountStatus": "xx",
"Id": "12314",
"AccountStatus": "YYC",
}
]}
},
|
But if there is less than 2 instances of recurring sub trees / elements IIB don't create JSON array but serializes them as object:
Quote: |
"ListOfAccount": {"Account": {
"ParentAccountId": "xx",
"ParentAccountName": "yy",
"ParentAccountStatus": "xx",
"Id": "123135",
"AccountStatus": "YYZ",
}}
}, |
This phenomenon annoys client application developers and they need to implement special parsing because of this.
--
Marko |
|
Back to top |
|
 |
smdavies99 |
Posted: Thu Dec 11, 2014 12:41 am Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
If the JSON is valid then the developers should consider switching parsers.
You can check the JSON online
http://jsonlint.com/ _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
kimbert |
Posted: Thu Dec 11, 2014 2:41 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
This is a common problem when converting from XML to JSON.
The problem is that JSON has a special syntax for an 'array' and XML does not. In general, an XML parser does not know that a tag has maxOccurs>1. So the only way to recognise an XML 'array' is to spot two consecutive tags with the same name.
Now, some XML processing systems do allow the parsed data to be correlated with the xsd ( using PSVI : http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29 ). That allows the maxOccurs property to be queried, and then arrays *can* be recognized. However, even then it is a tricky business to get right.
Currently there is no way to do this using standard IIB facilities. It would be possible to parse and validate the XML using a JavaCompute node and then use the Xerces PSVI to obtain the maxOccurs for each tag and output the correct JSON. But that would be overkill unless you have a lot of different JSON document styles. An easier solution would be to maintain a list of the array names and write some code that marks them all as JSON.Array. _________________ Before you criticize someone, walk a mile in their shoes. That way you're a mile away, and you have their shoes too. |
|
Back to top |
|
 |
marko.pitkanen |
Posted: Thu Dec 11, 2014 10:44 pm Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
Thanks Kimbert for clarifying the theory behind the phenomenon and providing great advices how to desing a solution for the challenge.
I think I will use this solution in this particular case.
kimbert wrote: |
An easier solution would be to maintain a list of the array names and write some code that marks them all as JSON.Array. |
--
Marko |
|
Back to top |
|
 |
akil |
Posted: Fri Dec 19, 2014 6:49 am Post subject: |
|
|
 Partisan
Joined: 27 May 2014 Posts: 338 Location: Mumbai
|
Theres a mobile / work light pattern that comes with IIB9, it generates XML2JSON.java and JSON2XML.java . These generated code has the design mentioned by kimbert implemented..  _________________ Regards |
|
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
|
|
|
|