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 235
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

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.

I raised a feature request to allow this in one step. ref: b/171917812

Not applicable

you can use extract variable and assign message policy as well.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="OrderId">
            <JSONPath>$.Order.OrderId</JSONPath>
        </Variable>
        <Variable name="ShippingGroupId">
            <JSONPath>$.Order.ShippingGroups.ShippingGroup.ShippingGroupId</JSONPath>
        </Variable>
        <Variable name="LineItemID1">
            <JSONPath>$.Order.ShippingGroups.ShippingGroup.LineItems[0].LineItem.LineItemID</JSONPath>
        </Variable>
        <Variable name="Quantity1">
            <JSONPath>$.Order.ShippingGroups.ShippingGroup.LineItems[0].LineItem.Quantity</JSONPath>
        </Variable>
        <Variable name="LineItemID2">
            <JSONPath>$.Order.ShippingGroups.ShippingGroup.LineItems[1].LineItem.LineItemID</JSONPath>
        </Variable>
        <Variable name="Quantity2">
            <JSONPath>$.Order.ShippingGroups.ShippingGroup.LineItems[1].LineItem.Quantity</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
</ExtractVariables>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/xml">
            <LineItems>
                <LineItem>
                    <LineItemID>{LineItemID1}</LineItemID>
                    <Quantity>{Quantity1}</Quantity>
                </LineItem>
                <LineItem>
                    <LineItemID>{LineItemID2}</LineItemID>
                    <Quantity>{Quantity2}</Quantity>
                </LineItem>
            </LineItems>
        </Payload>
    </Set>
</AssignMessage>