How to get a Json response by passing input in Headers of PostMan.

Hi All,

I have a usecase where to send request in header of PostMan and get the Json response.

I have been used Xsl and ExtractVariables policies.

Xslt code is:(XSL-Tranform.xsl)->

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">{
    <xsl:apply-templates select="*"/>} </xsl:template> 
<!-- Object or Element Property-->
<xsl:template match="*">
    "<xsl:value-of select="name()"/>" :<xsl:call-template name="Properties">
        <xsl:with-param name="parent" select="'Yes'"> </xsl:with-param>
    </xsl:call-template>
</xsl:template> <!-- Array Element --> <xsl:template match="*" mode="ArrayElement">
    <xsl:call-template name="Properties"/> </xsl:template><!-- Object Properties -->
<xsl:template name="Properties">
    <xsl:param name="parent"></xsl:param>
    <xsl:variable name="childName" select="name(*[1])"/>
    <xsl:choose>           
        <xsl:when test="not(*|@*)"><xsl:choose><xsl:when test="$parent='Yes'"> <xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text></xsl:when>
                <xsl:otherwise>"<xsl:value-of select="name()"/>":"<xsl:value-of  select="."/>"</xsl:otherwise>
            </xsl:choose>                    </xsl:when>               
        <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of  select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
        <xsl:otherwise>{
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*"/>
            }</xsl:otherwise>
    </xsl:choose>
    <xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template><!-- Attribute Property --> <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>

Extract Variables-1-> (In PreFlow of ProxyEndpoint)

<?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>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>VisitorsN</VariablePrefix>
    <XMLPayload stopPayloadProcessing="false">
        <Namespaces/>
        <Variable name="first_name" type="String">
            <XPath>/VisitorsN/VisitorA/first_name</XPath>
             <Step>
                <Condition>(VisitorsN.VisitorA.first_name = Gov)</Condition>
            </Step>
        </Variable>
        <Variable name="first_name" type="String">
            <XPath>/VisitorsN/VisitorB/first_name</XPath>
            <Step>
                <Condition>(VisitorsN.VisitorB.first_name = Amit)</Condition>
            </Step>
        </Variable>
    </XMLPayload>
</ExtractVariables>

xml input data(Request In body of PostMan):

<?xml version="1.0" encoding="UTF-8"?>
<VisitorsN> 
<VisitorA>
 <first_name>Gov</first_name> 
 <middle_name>Kr</middle_name>
 <Last_name>Verma</Last_name>
 <house_number>15</house_number>
 <road_namee>ratu road</road_namee>
 <pincode>239809709</pincode>
</VisitorA>
 <VisitorB>
 <first_name>Amit</first_name> 
 <Last_name>Singh</Last_name> 
 <house_number></house_number>
 <road_namee>Harmu</road_namee> 
 <pincode>398097692</pincode>
 </VisitorB>
</VisitorsN>

In Header of PostMan I am passing input as :

Key : first_name Value : Gov

Output data(response)is:

<?xml version="1.0" encoding="UTF-8"?>{     "VisitorsN" :{
    "VisitorA" :{
    "first_name" :"Govind",     
    "middle_name" :"Kr",     
    "Last_name" :"Verma",   
    "house_number" :"15",
    "road_namee" :"ratu road",   
    "pincode" :"239809709"
            },
     "VisitorB" :{
    "first_name" :"Amit",
    "Last_name" :"Singh",     
    "house_number" :"",     
    "road_namee" :"Harmu",     
    "pincode" :"398097692"
            }
            }} 

But i want output (response) as:

case1:

Key : first_name Value : Gov

Output should be:

<?xml version="1.0" encoding="UTF-8"?>{     "VisitorsN" :{
    "VisitorA" :{
    "first_name" :"Govind",     
    "middle_name" :"Kr",     
    "Last_name" :"Verma",   
    "house_number" :"15",
    "road_namee" :"ratu road",   
    "pincode" :"239809709"
            }}} 

case2:

Key : first_name Value : Amit

Output should be:

<?xml version="1.0" encoding="UTF-8"?>{     "VisitorsN" :{
     "VisitorB" :{
    "first_name" :"Amit",
    "Last_name" :"Singh",     
    "house_number" :"",     
    "road_namee" :"Harmu",     
    "pincode" :"398097692"
            }}}

Could someone please help me?

Any Advice what i'm doing wrong here?

Thanks and Regards

Govind Verma

@Anil Sagar @ Google, @Dino-at-Google,@Robert Johnson,@Brendan,@Siddharth Barahalikar, @deboraelkin,

0 3 1,903
3 REPLIES 3

You have an issue with the Extract Variables syntax. You can't use conditions within it (These are for proxy or target endpoint configuration.

I suggest you do the following:

BEFORE you convert the XML to JSON, run an XSLT transform to only leave the element(s) matching the first name:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml"/>
  <xsl:param name="firstName" />
  <xsl:template match="/">
    <VisitorsN>
      <xsl:for-each select="/child::*/child::*[first_name = $firstName]">
         <xsl:copy-of select="." />
      </xsl:for-each>
    </VisitorsN>
  </xsl:template>
</xsl:stylesheet>

This XSLT uses a parameter, so you can dynamically set the value for first_name

You'll need to set the parameter when configuring the XSLTransform policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XSL async="false" continueOnError="false" enabled="true" name="XSL-Transform-2">
    <DisplayName>XSL Transform-2</DisplayName>
    <Properties/>
    <Source>request</Source>
    <ResourceURL>xsl://XSL-Transform-2.xsl</ResourceURL>
    <Parameters ignoreUnresolvedVariables="true">
        <Parameter name="firstName" ref="message.header.first_name"/>
    </Parameters>
</XSL>

After running this XSLT transformation, the XML will now look like (assuming request.header.first_name = 'Amit'):

<?xml version="1.0" encoding="UTF-8"?>
<VisitorsN>
  <VisitorB>
   <first_name>Amit</first_name> 
   <Last_name>Singh</Last_name> 
   <house_number/>
   <road_namee>Harmu</road_namee> 
   <pincode>398097692</pincode>
  </VisitorB>
</VisitorsN>

You can now convert the xml into JSON. Again, I recommend you use an XML2JSON policy instead of running that XSL transformation. It just makes your life easier. 🙂

Thanks for your reply,

deboraelkin

But I didn't understand this. Did you mean i have to add 2 different XSL Policies and No need of Extract Variable?

Yes. You can also replace the 2nd XSL policy (the one you already have, which essentially transforms XSL to JSON) with an XML2JSON policy