Using variables to craft XML request from JSON using AssignMessage payload

I'm building an API that talks to a SOAP backend. The proxy accepts a JSON payload, and I need to convert that to XML. The issue I'm having is that the payload I'm getting is complex, so I can't simply extract a few variables from it and then build the XML payload. What I'd like to do is extract the large chunks of data from the JSON, use JSONtoXML to convert that to XML format, then extract what I need, and inject it into a custom XML payload using AssignMessage.

When I tried this, though, I couldn't figure out how to inject a block of XML into the message. For example, assume I get a payload like this:

// The actual data is much, much larger, and includes several levels of nesting
{
"foo": "bar", "baz": 12345
}

I can run this through a JSONtoXML policy and get this:

<?xml version="1.0" encoding="UTF-8"?><Root>  <foo>bar</foo>  <baz>12345</baz></Root>

What I want to do next, is be able to extract those two XML nodes, `foo` and `bar`, and use them in an AssignMessage like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Create-Auto-Request">
    <DisplayName>Create Auto Request</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="text/xml">
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://some.net/namespace">
                <soapenv:Header/>
                <soapenv:Body>
                    <v1:someService xmlns:v10="http://some.net/othernamespace">
                        <request>
                            <ServiceInformation>
                                <Environment>{request.header.environment}</Environment>
                            </ServiceInformation>
                            {foo}
			    {bar}
                            <SomeOtherInformation/>
                        </request>
                    </v1:someService>
                </soapenv:Body>
            </soapenv:Envelope>
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

When I try this, I get blanks. Is it even possible to use message templates in this context? Should I convert my JSON to XML strings using JavaScript?

Solved Solved
0 3 239
1 ACCEPTED SOLUTION

I believe for your approach to work, you would need to use Extract Variables to extract your individual json variables (ie so you'll have the actual values for foo and baz. Then you'll need to specify the xml structure in your assign message eg

<ServiceInformation>
   <foo>{fooValue}</foo>
   <baz>{bazValue}</baz>
</SomeOtherInformation>

For what you're trying to do, I would instead consider looking at using the XSLT policy after you do the initial JSON -> XML conversion. It looks like you want to do more specific mapping of an XML structure, where as the assign message with the variables is better suited for building strings

View solution in original post

3 REPLIES 3

I believe for your approach to work, you would need to use Extract Variables to extract your individual json variables (ie so you'll have the actual values for foo and baz. Then you'll need to specify the xml structure in your assign message eg

<ServiceInformation>
   <foo>{fooValue}</foo>
   <baz>{bazValue}</baz>
</SomeOtherInformation>

For what you're trying to do, I would instead consider looking at using the XSLT policy after you do the initial JSON -> XML conversion. It looks like you want to do more specific mapping of an XML structure, where as the assign message with the variables is better suited for building strings

Thanks! I'll give those approaches a try. I was hoping to avoid the XSLT route, but that may be the best option available.

I agree; for generating dynamic SOAP messages, XSLT is the best option. (sorry. But your fate was sealed when you used SOAP)