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 SupportIssues with JSON to XML Conversion

Post new topicReply to topic
Issues with JSON to XML Conversion View previous topic :: View next topic
Author Message
nik_1975
PostPosted: Fri Oct 05, 2018 3:37 am Post subject: Issues with JSON to XML Conversion Reply with quote

Newbie

Joined: 16 Mar 2017
Posts: 1

Hi,

Using below ESQL for Converting JSON to XML

Code:
CREATE PROCEDURE createXMLArrays(IN root REFERENCE )
        BEGIN
                -- track the start and end of 'arrays' ( sequences of same-named siblings )
                DECLARE firstArrayElementRef REFERENCE TO root;
                DECLARE siblingRef REFERENCE TO root;
                DECLARE arraySize INTEGER 0;

                WHILE LASTMOVE(siblingRef) DO
                       
                        -- Process any child elements first
                        DECLARE firstChildRef REFERENCE TO siblingRef;
                        MOVE firstChildRef FIRSTCHILD;
                        IF LASTMOVE(firstChildRef) THEN
                                CALL createxmlArrays(firstChildRef);
                        END IF;

                        -- IF this sibling's name is different from the previous one.
                        IF FIELDNAME(siblingRef) = FIELDNAME(firstArrayElementRef) THEN
                                SET arraySize = arraySize + 1;
                        ELSE
                                -- IF there was a sequence of two or more siblings with the same name
                                IF arraySize > 1 THEN
                                        CALL createXMLArray(firstArrayElementRef, arraySize);
                                END IF;                         

                                -- start scanning for a new array
                                MOVE firstArrayElementRef TO siblingRef;
                                SET arraySize = 1;
                        END IF;
                       
                        MOVE siblingRef NEXTSIBLING;
                END WHILE;
               
                IF arraySize > 1 THEN
                        CALL createXMLArray(firstArrayElementRef, arraySize);
                END IF;
        END;

        CREATE PROCEDURE createXMLArray(IN firstArrayElementRef REFERENCE, IN arraySize INTEGER) BEGIN
                -- Create a parent element for the array
                DECLARE arrayParentRef REFERENCE TO firstArrayElementRef;
                DECLARE arrayParentExtraRef1 REFERENCE TO firstArrayElementRef;
                MOVE arrayParentExtraRef1 PARENT;
                DECLARE arrayParentExtraRefT REFERENCE TO arrayParentExtraRef1;
                 DECLARE arrayParentExtraRefTcON REFERENCE TO arrayParentExtraRef1;       
                -- Make the array members children of the new parent element
                DECLARE pos INTEGER 1;
                WHILE pos < arraySize DO
                   CREATE NEXTSIBLING OF arrayParentExtraRef1
                        AS arrayParentExtraRefT
                        NAME FIELDNAME(arrayParentExtraRef1);
                       
                        DECLARE tempRef REFERENCE TO firstArrayElementRef;
                        -- advance the reference. This should never fail because we have
                        -- already walked all of these siblings to discover the array.
                        MOVE firstArrayElementRef NEXTSIBLING;
                  MOVE arrayParentExtraRef1 NEXTSIBLING;
                   
                        DETACH tempRef;
                        --ATTACH tempRef.Item TO arrayParentExtraRefT AS LASTCHILD;
                        SET arrayParentExtraRefT = tempRef;
                        SET pos = pos + 1;
                END WHILE;
                SET arrayParentExtraRefTcON = firstArrayElementRef;
                DETACH arrayParentExtraRefTcON;
                ATTACH arrayParentExtraRefTcON TO arrayParentExtraRefT AS NEXTSIBLING;
        END;


The Problem is :

When JSON Request Array (product) is single as below :

Code:
{
   "customerID": "12345",
   "caseSubType": "03",
   "sourceId": "333",
   "product": [
      {
         "type": "111",
         "subType": "111",
         "variant": "111"
      }
   ]
}


The XML is getting transformed as :

Code:
<Data>
 <customerID>12345</customerID>
 <caseSubType>03</caseSubType>
 <sourceId>333</sourceId>
 <product>
  <Item>
   <type>111</type>
   <subType>111</subType>
   <variant>111</variant>
  </Item>
 </product>
</Data>


The XML Should be :

Code:
<Data>
 <customerID>12345</customerID>
 <caseSubType>03</caseSubType>
 <sourceId>333</sourceId>
 <product>
   <type>111</type>
   <subType>111</subType>
   <variant>111</variant>
 </product>
</Data>


Can you please advise...
Back to top
View user's profile Send private message
timber
PostPosted: Fri Oct 05, 2018 4:28 am Post subject: Reply with quote

Sentinel

Joined: 25 Aug 2015
Posts: 856

First piece of advice is...when asking for help from strangers on the internet, why not take time to format your code and your data so that we can read it easily?

Why do you want a single-element JSON array to be treated differently from a multi-element JSON array. Sounds like a strange requirement.
Back to top
View user's profile Send private message
Display posts from previous:
Post new topicReply to topic Page 1 of 1

MQSeries.net Forum IndexWebSphere Message Broker SupportIssues with JSON to XML Conversion
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.