How to convert JSON to XML in the simple mode by Assign Message

Transforming Json payload request to XML calls on the backend a few minutes step-by-step.


- Create a API Proxy;

- Add a police (extract variables) to extract data and validated; You will to use the <VariablePrefix> tag in the police Assign Message.

<JSONPayload>

        <Variable name="variableName">

            <JSONPath>$.tagNameJson</JSONPath>

        </Variable>

 </JSONPayload>
<Source clearPayload="false">request</Source>
<VariablePrefix>procflow</VariablePrefix>

- Add a police (Raise Fault) to return the message error and put <FaultResponse> tag ;

<Payload contentType="application/json">{

              "error" : {

                "code" : 400,

                "message" : "Error field"

                  }

                }

</Payload>
<StatusCode>400</StatusCode>
<ReasonPhrase>message error field</ReasonPhrase>

- Add the police Assign Message, put the code follow:

<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-HeaderESB">
   <DisplayName>AM-HeaderESB</DisplayName>
   <Properties/>
   <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
   <AssignTo createNew="true" transport="http" type="request"/>
   <Set>
      <Headers>
         <Header name="Content-Type">text/xml; charset=utf-8</Header>
         <Header name="SOAPAction">http://urlteste/TS/PriceSynchronizationTSV2/syncPrice</Header>
      </Headers>
      <Payload contentType="text/xml">
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://urlteste/TS/PriceSynchronizationTSV2">
            <soapenv:Header/>
            <soapenv:Body>
               <v1:syncPriceRequest>
                  <v1:Item>
                     <v1:Operation>{procflow.variableName}</v1:Operation>                     <v1:PriceListName>Preco de Catalogo</v1:PriceListName>
                  </v1:Item>
               </v1:syncPriceRequest>
            </soapenv:Body>
         </soapenv:Envelope>
      </Payload>
      <Verb>POST</Verb>
   </Set>
</AssignMessage>

The Tags <Headers>, <Payload> and <Verb>, must be completed according to the example above. Don't forget to use SOAPAction in the header name, the POST method in the verb, and define the XML payload. See that the Police Extract VariablePrefix has been included in the XML payload so that APIGEE gets the value of the variable

See that the proxy Endpoint the following steps should be placed as below in the sequence <Request> PreFlow.

<PreFlow name="PreFlow">
<Request>
 <Step>
  <Name>EX-PostDatasCustomer</Name> <!-- police extract variables-->
 </Step>
 <Step>
  <Name>validate</Name> <!-- police raise fault-->
  <Condition>{procflow.variableName is null || procflow.variableName = ""}</Condition>
 </Step>
 <Step>
  <Name>AM-HeaderESB</Name> <!-- police assign message-->
 </Step>
</Request>
<Response/>
</PreFlow>

- Add a police XMLToJson for your response payload to be transform in json.

<XMLToJSON async="false" continueOnError="false" enabled="true" name="XJ-Transform">
    <DisplayName>XJ-Transform</DisplayName>
    <Properties/>
    <OutputVariable>response</OutputVariable>
    <Source>response</Source>
    <Options>
        <StripLevels>2</StripLevels>
    </Options>
</XMLToJSON>

- See that o Target Endpoint, has a statement:

<PreFlow name="PreFlow">
        <Request/>
        <Response>
            <Step>
                <Name>XJ-Transform</Name> <!-- police XMLToJson -->
            </Step>
        </Response>
    </PreFlow>

- Another tip. If you want in the proxy, use LoadBalancer, put the parameter in the Apigee configuration (targetServer) and define it in the tag below.

When working with the production, approval and development environments, the application of this rule is very productive.

<HTTPTargetConnection>

        <Properties/>

        <LoadBalancer>

            <Server name=“parm-esb"/>

        </LoadBalancer>

        <Path>/TS/PriceSyn</Path>

</HTTPTargetConnection>
<br />

thanks a lot, bye bye! @fabriziorodrigues

.

Version history
Last update:
‎02-07-2020 11:27 AM
Updated by: