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 » XML to JSON help with JSON Arrays needed

Post new topic  Reply to topic
 XML to JSON help with JSON Arrays needed « View previous topic :: View next topic » 
Author Message
marko.pitkanen
PostPosted: Thu Dec 11, 2014 12:20 am    Post subject: XML to JSON help with JSON Arrays needed Reply with quote

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
View user's profile Send private message Visit poster's website
smdavies99
PostPosted: Thu Dec 11, 2014 12:41 am    Post subject: Reply with quote

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
View user's profile Send private message
kimbert
PostPosted: Thu Dec 11, 2014 2:41 am    Post subject: Reply with quote

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
View user's profile Send private message
marko.pitkanen
PostPosted: Thu Dec 11, 2014 10:44 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
akil
PostPosted: Fri Dec 19, 2014 6:49 am    Post subject: Reply with quote

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

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » XML to JSON help with JSON Arrays needed
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.