Is there a way to use a variable to set up the Source and OutputVariable of an XMLtoJSON policy?

I'm trying to call an XMLtoJSON policy on 2 locations of my code and I wanted to know if there was a way to set the Source and the OutputVariable with one variable I tried this but it is not working 

<XMLToJSON name="ConvertToJSON">
 
<Options>
 
</Options>
 
<OutputVariable>{variable}</OutputVariable>
 
<Source>{variable}</Source>
</XMLToJSON>

Edited: 
Didn't explained correctly, this {variable} contains an string with response or request, so the question was if I can use this variable to make a dynamic policy

 

Solved Solved
0 4 138
1 ACCEPTED SOLUTION

Ah, ok. I think I understand now.

The variable(s) you give to OutputVariable and Source should not be in curly brackets. If you want it to intake the "response or request depending on the place where it is called", consider utilizing the default behavior of Source "If <Source> is not defined, then it is treated as message (which resolves to request when the policy is attached to a request flow, or response when the policy is attached to a response flow)". OutputVariable will follow suit to match Source if omitted as well.

If your conditional logic is more complex than that, consider using conditional flows  to switch between two different XMLtoJSON policies for request and response. 

<Flow name="ReqFlow">
    <Description/>
    <Request>
        <Step>
            <Name>X2J-req</Name>
        </Step>
    </Request>
    <Response/>
    <Condition>proxy.pathsuffix MatchesPath "/foo"</Condition>
</Flow>
<Flow name="RespFlow">
    <Description/>
    <Request/>
    <Response>
        <Step>
            <Name>X2J-resp</Name>
        </Step>
    </Response>
    <Condition>proxy.pathsuffix MatchesPath "/bar"</Condition>
</Flow>

 Lastly, you can use the XMLtoJSON policy with the variable of your choice so long as the variable is of message type. In the below example I use an AssignMessage policy to create a message variable called "altReq". Then I use XMLtoJSON to execute on that variable instead of the standard request or response variable

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-createMessage">
  <DisplayName>AM-createMessage</DisplayName>
  <AssignTo createNew="true" transport="http" type="request">altReq</AssignTo>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Verb>POST</Verb>
    <Payload contentType="application/xml">
      <root>
        <foobar/>
      </root>
    </Payload>
    <StatusCode>200</StatusCode>
    <ReasonPhrase>OK</ReasonPhrase>
  </Set>
</AssignMessage>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON continueOnError="false" enabled="true" name="X2J-altReq">
  <DisplayName>X2J-altReq</DisplayName>
  <Properties/>
  <Format>yahoo</Format>
  <OutputVariable>altReq</OutputVariable>
  <Source>altReq</Source>
</XMLToJSON>

 

View solution in original post

4 REPLIES 4

Yes, you can set the Source and the OutputVariable with one variable. Here's a sample from the documentation:

 

<XMLToJSON name="ConvertToJSON">
  <Options>
  </Options>
  <OutputVariable>response</OutputVariable>
  <Source>response</Source>
</XMLToJSON>

 

If you again look at the documentation, the Source and OutputVariable elements must be message data types. And to be clear, the XML that you convert is held within the payload of the given request or response message.

Edited: I should note that while Source and OutputVariable require message data types, the variable doesn't need to be the request or response. You can create your own message data type variable using AssignMessage. See example #3 under AssignTo.

Maybe I didn't explained myself correctly, what I meant was if there was a way to set this source and outputvariable dynamicly using a variable string something like 

<XMLToJSON name="ConvertToJSON">
 
<Options>
 
</Options>
 
<OutputVariable>{X}</OutputVariable>
 
<Source>{X}</Source>
</XMLToJSON> 

 with x in this case containing the string response or request depending on the place where it is called

Ah, ok. I think I understand now.

The variable(s) you give to OutputVariable and Source should not be in curly brackets. If you want it to intake the "response or request depending on the place where it is called", consider utilizing the default behavior of Source "If <Source> is not defined, then it is treated as message (which resolves to request when the policy is attached to a request flow, or response when the policy is attached to a response flow)". OutputVariable will follow suit to match Source if omitted as well.

If your conditional logic is more complex than that, consider using conditional flows  to switch between two different XMLtoJSON policies for request and response. 

<Flow name="ReqFlow">
    <Description/>
    <Request>
        <Step>
            <Name>X2J-req</Name>
        </Step>
    </Request>
    <Response/>
    <Condition>proxy.pathsuffix MatchesPath "/foo"</Condition>
</Flow>
<Flow name="RespFlow">
    <Description/>
    <Request/>
    <Response>
        <Step>
            <Name>X2J-resp</Name>
        </Step>
    </Response>
    <Condition>proxy.pathsuffix MatchesPath "/bar"</Condition>
</Flow>

 Lastly, you can use the XMLtoJSON policy with the variable of your choice so long as the variable is of message type. In the below example I use an AssignMessage policy to create a message variable called "altReq". Then I use XMLtoJSON to execute on that variable instead of the standard request or response variable

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-createMessage">
  <DisplayName>AM-createMessage</DisplayName>
  <AssignTo createNew="true" transport="http" type="request">altReq</AssignTo>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Verb>POST</Verb>
    <Payload contentType="application/xml">
      <root>
        <foobar/>
      </root>
    </Payload>
    <StatusCode>200</StatusCode>
    <ReasonPhrase>OK</ReasonPhrase>
  </Set>
</AssignMessage>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON continueOnError="false" enabled="true" name="X2J-altReq">
  <DisplayName>X2J-altReq</DisplayName>
  <Properties/>
  <Format>yahoo</Format>
  <OutputVariable>altReq</OutputVariable>
  <Source>altReq</Source>
</XMLToJSON>

 

Thank you so much, I was just checking if there was a way to set it up, the problem I had was that one of my messages is part of the fault rules error messages so it does not work with the default behaviour of Source, I'll just make 2 different policies.
I wanted to make use of the shared flows to apply the XMLtoJSON to 2 different proxies while also being able to reuse it through different parts of the same proxy and I wasn't sure if it could be done that way.