I am using apigee x, I have an XML request I want to convert.
I am using extract variables to get the userid and ordernumbers, I am then attempting to reformat the request to JSON to call my new backend server with its new format.
<orderrequest>
<userid>1238123abc</userid>
<ordernumbers>
<ordernumber>1234568690</ordernumber>
<ordernumber>1234568690</ordernumber>
</ordernumbers>
</orderrequest>
I am extracting the order numbers as {orderrequest.ordernumbers} using an xpath selector
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-XML-REQUEST">
<DisplayName>EV-XML-REQUEST</DisplayName>
<Source clearPayload="false">request</Source>
<VariablePrefix>orderrequest</VariablePrefix>
<XMLPayload stopPayloadProcessing="false">
<Namespaces/>
<Variable name="ordernumbers" type="string">
<XPath>/orderrequest/ordernumbers</XPath>
</Variable>
</XMLPayload>
</ExtractVariables>
I am then trying to output this as json with a assign message set override
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-1">
<DisplayName>Assign Message-1</DisplayName>
<Set>
<Payload>
{ "orders": "{orderrequest.ordernumbers}" }
</Payload>
<Headers>
<Header name="content-type">
application/json
</Header>
<Header name="accept">
application/json
</Header>
</Headers>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="true" transport="http" type="request"/>
</AssignMessage>
however the {orderrequest.ordernumbers} is not what I expect at all and returns the following string:
{
"orders": "
1234568690
1234568690
" }
how I do make this as intended an array of strings? e.g
{
"orders": [
"1234568690",
"1234568690"
]
}
Solved! Go to Solution.
You don't need to use the ExtractVariables policy. There is a better way to convert XML payloads into JSON, in Apigee: Use the XMLToJSON policy.
For example, with this input payload
<orderrequest>
<userid>1238123abc</userid>
<ordernumbers>
<ordernumber>1234568690</ordernumber>
<ordernumber>2345686901</ordernumber>
</ordernumbers>
</orderrequest>
...this policy configuration
<XMLToJSON name='X2J-1'>
<Source>message</Source>
<OutputVariable>message.content</OutputVariable>
<Options>
<TreatAsArray>
<!-- unwrap='true' means omit the inner element -->
<Path unwrap='true'>orderrequest/ordernumbers/ordernumber</Path>
</TreatAsArray>
</Options>
</XMLToJSON>
results in converting the message to json, of this form:
{
"orderrequest": {
"userid": "1238123abc",
"ordernumbers": [
"1234568690",
"2345686901"
]
}
}
...which is I think what you want. If you want to omit the orderrequest.userid ... and just get the ordernumbers array in json , you will need to use JavaScript or ... jsonPath within an AssignMessage.
You don't need to use the ExtractVariables policy. There is a better way to convert XML payloads into JSON, in Apigee: Use the XMLToJSON policy.
For example, with this input payload
<orderrequest>
<userid>1238123abc</userid>
<ordernumbers>
<ordernumber>1234568690</ordernumber>
<ordernumber>2345686901</ordernumber>
</ordernumbers>
</orderrequest>
...this policy configuration
<XMLToJSON name='X2J-1'>
<Source>message</Source>
<OutputVariable>message.content</OutputVariable>
<Options>
<TreatAsArray>
<!-- unwrap='true' means omit the inner element -->
<Path unwrap='true'>orderrequest/ordernumbers/ordernumber</Path>
</TreatAsArray>
</Options>
</XMLToJSON>
results in converting the message to json, of this form:
{
"orderrequest": {
"userid": "1238123abc",
"ordernumbers": [
"1234568690",
"2345686901"
]
}
}
...which is I think what you want. If you want to omit the orderrequest.userid ... and just get the ordernumbers array in json , you will need to use JavaScript or ... jsonPath within an AssignMessage.