Extract variable policy -- set default value to variable

Not applicable

I have a Extract variable policy in my proxy, which is extracting values from JSON payload and saving it in variable. I want to set a default value for one of the boolean variable I have. based on Apigee docs I see the only attribute variable has is "name". Is there a way to set some default value to variable for which no pattern is found in JSON?

0 3 2,249
3 REPLIES 3

Hi @Merolin Christie - You can use an Assign Message policy or Javascript policy.

If you are using the latter, you can set the variable using the context object - something like

context.setVariable("nameOfBooleanVar", true);

Hope this helps

based on Apigee docs I see the only attribute variable has is "name".

Not clear.

I think you want to assign a value to the variable if the ExtractVariable does not find a pattern. Is that right?

Not sure you can do *that*, but there are two possible workarounds.

1. use an assignmessage to assign a default value, and run that policy before the ExtractVariables policy. The code would look like this:

<AssignMessage AM-1'>
  <AssignVariable>
    <Name>my_variable</Name>
    <Value>default-goes-here</Value>
  </AssignVariable>
</AssignMessage>

2. If you do not wish to pre-assign the variables, you can substitute default values for unassigned variables, later, in variable substitution expressions.

For example, suppose the variable with the name 'my_variable' has no value. It has not been assigned. Then, using this syntax:

<AssignMessage name='AM-1'>
    <Set>
      <Payload contentType='application/json'>{
  "value" : "{my_variable:defaultvalue}"
}
</Payload>
        <StatusCode>200</StatusCode>
        <ReasonPhrase>OK</ReasonPhrase>
    </Set>
    <AssignTo createNew='false' transport='http' type='response'/>
</AssignMessage>

...will result in a payload like this being assigned:

{
  "value" : "defaultvalue"
}

If my_variable held the value 'foo', then this payload would be assigned:

{
  "value" : "foo"
}

Does that help?