Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

JSONtoXML Making array multiple elements

We have a client sending a request similar to the following:

{
    "Order": {
        "OrderId": "00001282",
        "ShippingGroups": {
            "ShippingGroup": {
                "ShippingGroupId": "sh00005502",
                "LineItems": [
                    {
                        "LineItem": {
                            "LineItemID": "plida8055335850643b1aa9331ee3",
                            "Quantity": "5"
                        }
                    },
                    {
                        "LineItem": {
                            "LineItemID": "plia762acad1a4e500029c72590dd",
                            "Quantity": "5"
                        }
                    }
                ]
            }
        }
    }
}

We are running it through the following JSONtoXML policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONToXML enabled="true" continueOnError="false" async="false" name="JSONToXMLPolicy">
    <DisplayName>JSONToXMLPolicy</DisplayName>
    <FaultRules/>
    <Properties/>
    <Options>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
        <NamespaceSeparator>:</NamespaceSeparator>
    </Options>
    <OutputVariable>request</OutputVariable>
    <Source>request</Source>
</JSONToXML>

The resulting xml converts $..LineItems.LineItem to something like this:

<LineItems>
    <LineItem>
        <LineItemID>plida8055335850643b1aa9331ee3</LineItemID>
        <Quantity>5</Quantity>
    </LineItem>
</LineItems>
<LineItems>
    <LineItem>
        <LineItemID>plia762acad1a4e500029c72590dd</LineItemID>
        <Quantity>5</Quantity>
    </LineItem>
</LineItems>

However, what we would like it to look like is below. But that doesn't seem to do it. Is there a clean solution to this? Does it require an xsl transform?

<LineItems>
    <LineItem>
        <LineItemID>plida8055335850643b1aa9331ee3</LineItemID>
        <Quantity>5</Quantity>
    </LineItem>
    <LineItem>
        <LineItemID>plia762acad1a4e500029c72590dd</LineItemID>
        <Quantity>5</Quantity>
    </LineItem>
</LineItems>
Solved Solved
0 3 268
1 ACCEPTED SOLUTION

I don't know of a way to do this today, with just one step. That leaves you these options:

- a JavaScript step BEFORE the JSONToXML that transforms the JSON into something different

- an XSL step AFTER the JSONToXML to transform the XML into something different

I think it's a reasonable feature request to ask that the array result in your desired output. Unfortunately that isn't the behavior today and there's no option to get that behavior today. So I think you'll need to explicitly work around that.

View solution in original post

3 REPLIES 3