|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Issues with JSON to XML Conversion |
« View previous topic :: View next topic » |
Author |
Message
|
nik_1975 |
Posted: Fri Oct 05, 2018 3:37 am Post subject: Issues with JSON to XML Conversion |
|
|
Newbie
Joined: 16 Mar 2017 Posts: 4
|
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 |
|
 |
timber |
Posted: Fri Oct 05, 2018 4:28 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
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 |
|
 |
|
|
 |
|
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
|
|
|
|