Using SubString in Extract Variable

I want to extract employee number from astring. The string format will be

123456@abc.com

I want to extract number before the '@' . characters after '@' may vary for employees and is not same for all employees

I am using EV policy and storing username in variable, and username will have value:123456@abc.com

 <JSONPayload>
        <Variable name="username">
            <JSONPath>username</JSONPath>
        </Variable>
    </JSONPayload>
Solved Solved
0 8 502
1 ACCEPTED SOLUTION

That won't work... The 2nd Variable element will not work as you might expect. I believe there is a bug there, or at the least it is a surprising behavior. A single ExtractVariables policy is not able to first extract a value from a payload, and then extract a value from the variable that was just set. You can do what you want if you separate those steps into 2 distinct EV policies....

policy1

<ExtractVariables name='EV-from-JSON-Payload-1'>
  <Source>contrivedIntrospectionResponse</Source>
  <VariablePrefix>extracted</VariablePrefix>
  <JSONPayload>
    <Variable name='username'>
      <JSONPath>$.username</JSONPath>
    </Variable>
  </JSONPayload>
  <!-- this won't work -->
   <Variable name="extracted.username">
       <Pattern>{empid}@**</Pattern>
   </Variable>
  <!-- this also won't work -->
   <Variable name="username">
       <Pattern>{empid2}@**</Pattern>
   </Variable>
</ExtractVariables>

policy2

<ExtractVariables name='EV-from-Variable'>
  <VariablePrefix>extracted</VariablePrefix>
  <Variable name="extracted.username">
       <Pattern>{empid2}@**</Pattern>
  </Variable>
</ExtractVariables>

View solution in original post

8 REPLIES 8

Not applicable

You have not specified where the variable is present, in a variable or json body. If it's in a variable follow below code.

<Variable name="variable which contains full string">
       <Pattern>{empid}@**</Pattern>
   </Variable>

empid will have the employee id in it.

Hi,

It is in the json payload. it is in the response of one of the service call out.

{
 "active": true,
 "scope": "openid email profile",
 "username": "123456@abc.com",
 "exp": 1212,
 "iat": 1212,
}

Not applicable

Then, use as below

   <JSONPayload>
      <Variable name="emailid" type="string">
         <JSONPath>$.username</JSONPath>
      </Variable>
      
   </JSONPayload>

   <Variable name="emailid">
       <Pattern>{empid}@**</Pattern>
   </Variable>

When I use below code the 'emailid' variable is showing blank in trace.

my username is : "username": "123456@abc.tkp.com",

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="checkOktaResponse">
    <DisplayName>checkOktaResponse</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>        
        <Variable name="username">
            <JSONPath>username</JSONPath>
        </Variable>
        <Variable name="emailid" type="string">
            <JSONPath>$.username</JSONPath>
        </Variable>
    </JSONPayload>
    <Variable name="emailid">
        <Pattern>{emplid}@**</Pattern>
    </Variable>
    <Source clearPayload="false">okta.introspection.response</Source>
</ExtractVariables>

There is a bug in ExtractVariables. Or maybe it is not a bug, it is just working-as-designed. Here it is: the policy performs all Variable extracts before any JSONPayload extract. Therefore you cannot do things in this order, in one policy:

  1. extract a field from a JSON payload into a variable,
  2. Extract something from that variable

You can do it in two distinct policies.

Policy1

<ExtractVariables name="checkOktaResponse">
    <JSONPayload>        
        <Variable name="emailid" type="string">
            <JSONPath>$.username</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">okta.introspection.response</Source>
</ExtractVariables>

policy2

<ExtractVariables name='EV-from-Variable'>
  <Variable name="emailid">
       <Pattern>{shortform}@**</Pattern>
  </Variable>
</ExtractVariables>

That won't work... The 2nd Variable element will not work as you might expect. I believe there is a bug there, or at the least it is a surprising behavior. A single ExtractVariables policy is not able to first extract a value from a payload, and then extract a value from the variable that was just set. You can do what you want if you separate those steps into 2 distinct EV policies....

policy1

<ExtractVariables name='EV-from-JSON-Payload-1'>
  <Source>contrivedIntrospectionResponse</Source>
  <VariablePrefix>extracted</VariablePrefix>
  <JSONPayload>
    <Variable name='username'>
      <JSONPath>$.username</JSONPath>
    </Variable>
  </JSONPayload>
  <!-- this won't work -->
   <Variable name="extracted.username">
       <Pattern>{empid}@**</Pattern>
   </Variable>
  <!-- this also won't work -->
   <Variable name="username">
       <Pattern>{empid2}@**</Pattern>
   </Variable>
</ExtractVariables>

policy2

<ExtractVariables name='EV-from-Variable'>
  <VariablePrefix>extracted</VariablePrefix>
  <Variable name="extracted.username">
       <Pattern>{empid2}@**</Pattern>
  </Variable>
</ExtractVariables>

Hi Dino,

It works as suggested by you. Create two policies and now getting employee number in the variable named extracted.empid2

Yes, in one policy these two operations are not working but in separate each works.