I want to extract 3 fields from my json response which returns array .

Hi all,

I am getting a json in response out of which I want to filter out 3 fields from the array json response.

I tried using extract variable policy and assign message policy but its not returning the expected output.

In attached zip- 1. initial json response 2. current filtered json response 3. expectedrestResult.

Thanks in advance!!

Extract Variable Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="get-response-soap-body">
    <DisplayName>Get Response SOAP Body</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="PROVIDER_NO">
            <JSONPath>$[*].Envelope.Body.LoadPolicyProviderResponse.LoadPolicyProviderResult.oPROVIDERS_DOL.PROVIDERS_DOL.PROVIDER_NO</JSONPath>
        </Variable>
        <Variable name="PROVIDER_NAME">
            <JSONPath>$[*].Envelope.Body.LoadPolicyProviderResponse.LoadPolicyProviderResult.oPROVIDERS_DOL.PROVIDERS_DOL.PROVIDER_NAME</JSONPath>
        </Variable>
        <Variable name="PROVIDER_TYPE">
            <JSONPath>$[*].Envelope.Body.LoadPolicyProviderResponse.LoadPolicyProviderResult.oPROVIDERS_DOL.PROVIDERS_DOL.PROVIDER_TYPE</JSONPath>
        </Variable>
    </JSONPayload>
    <Source>response</Source>
    <VariablePrefix>soapresponse</VariablePrefix>
</ExtractVariables>

Assign Message Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Final Response</DisplayName>
    <Properties/>
    <Set>
        <Payload>[{
            "ProviderID":"{soapresponse.PROVIDER_NO}",
            "ProviderName":"{soapresponse.PROVIDER_NAME}",
            "ProviderType":"{soapresponse.PROVIDER_TYPE}",
        }]
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMeresult.zipssage>
0 5 696
5 REPLIES 5

Structure is like:

Input json-

{ "Providers_DOL":

{"A":"abc","B":"bbb","C":"ccc", l, m ,n},

{"D":"ddd","E":"eee","F":"fff", o,p,q},

{"G":"ggg","H":"hhh","I":"iii",r,s,t}, , , }

Expected output json-

{"Service Providers":[

{ ""A":"abc", "B":"bbb", "C":"ccc" },

{ "D":"ddd", "E":"eee", "F":"fff" },

{ "G":"ggg", "H":"hhh", "I":"iii" }

] }

sujnana
Participant IV

I think it is not possible in Extract Variable and Assign Message combination. You can do this using Java Script callout. The json response can be modified in javascript the way you wanted.

Thanks @Sujnana.. It worked using Javascript policy.

Not applicable

is the array size fixed to 3?

I think you are starting with a SOAP XML response, and you want to get a JSON response from that, while dropping many of the elements in the SOAP response. Your ZIP file showed the "initial json response.txt", but that, to me, looks like the result of a XMLToJSON policy, where the input is a large soap message, something like this:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <soap:Header>
    <soap:Action mustUnderstand='1'>http://tempuri.org/IPOLICY/LoadPolicyProviderResponse</soap:Action>
  </soap:Header>
  <soap:Body>
    <LoadPolicyProviderResponse>
      <LoadPolicyProviderResult>
        <Status>
          <_Reason>
            <nil>true</nil>
            <TEXT>NULL</TEXT>
          </_Reason>
          <_ReasonDate>2020-07-28T12:31:30.6822027+03:00</_ReasonDate>
          <_StatusCode>1</_StatusCode>
        </Status>
        <oPROVIDERS_DOL>
          <PROVIDERS_DOL>
            <CreatedBy></CreatedBy>
            <CreationDate>0001-01-01T00:00:00</CreationDate>
            ...
          </PROVIDERS_DOL>
          ...
        </oPROVIDERS_DOL>
      </LoadPolicyProviderResult>
    </LoadPolicyProviderResponse>
  </soap:Body>
</soap:Envelope>

If that's the case, then I suggest that you first use XSL to convert the inbound XML to a form that is smaller, removes the soap headers, and strips all except the three elements you care about. (ProviderName, ProviderId, ProviderType)

Then run THAT resulting XML through a XMLToJSON policy to get the desired JSON document.

My XSL policy

<XSL name='XSL-1'>
  <DisplayName>XSL-1</DisplayName>
  <Source>request</Source>
  <ResourceURL>xsl://1.xsl</ResourceURL>
</XSL>

My XSL

<stylesheet version="2.0"
            xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns="http://www.w3.org/1999/XSL/Transform">


  <output method="xml"
          encoding="utf-8"
          indent="yes"
          omit-xml-declaration="yes"
          xslt:indent-amount="2" xmlns:xslt="http://xml.apache.org/xslt" />
  <strip-space elements="*"/>


  <template match="soap:Header"/>


  <template match="LoadPolicyProviderResponse">
    <apply-templates select="@*|node()"/>
  </template>


  <template match="LoadPolicyProviderResult">
    <apply-templates select="@*|node()"/>
  </template>
  <template match="Status"/>


  <template match="oPROVIDERS_DOL">
    <element name='root' namespace=''>
      <apply-templates select="node()|@*"/>
    </element>
  </template>


  <template match="PROVIDERS_DOL">
    <element name='ServiceProviders' namespace=''>
      <element name='ProviderID' namespace=''>
        <value-of select='PROVIDER_NO'/>
      </element>
      <element name='ProviderName' namespace=''>
        <value-of select='PROVIDER_NAME'/>
      </element>
      <element name='ProviderType' namespace=''>
        <value-of select='PROVIDER_TYPE'/>
      </element>
    </element>
  </template>


</stylesheet>


My XMLToJSON

<XMLToJSON name='X2J-1'>
  <Source>request</Source>
  <OutputVariable>request.content</OutputVariable>
  <Options>
      <StripLevels>1</StripLevels>
  </Options>
</XMLToJSON>

My output:

{"ServiceProviders":[
 {
  "ProviderID":"796604",
  "ProviderName":"Riyadh Consultative Clinic",
  "ProviderType":"0"
  },
  {
   "ProviderID":"796639",
   "ProviderName":"Al Belad Medical Center",
   "ProviderType":"0"
  },
  ...