Capture partial value from JSON payload

In the JSON payload like below, I have to capture "severity": only '4' digit value but ExtractVariables gives Sev4 , and which 4 digit value needs to be sent to other JSON pay load via <Set> <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">

{
  "schemaId": "azureMonitorCommonAlertSchema",
  "data": {
    "essentials": {
      "alertId": "/subscriptions/000000",
      "severity": "Sev4",
      ...
    }
    ...
  }
}

 

So is there a way to capture the partial value and save it to the parameter for passing to another JSON 

 

1 3 269
3 REPLIES 3

I guess you are using something like this in ExtractVariables, to get the severity field:

 

<ExtractVariables name='EV-Severity'>
  <Source>azureResponse</Source>
  <VariablePrefix>extracted</VariablePrefix>
  <JSONPayload>
    <Variable name='severity'>
       <JSONPath>$.data.essentials.severity</JSONPath>
    </Variable>
  </JSONPayload>
</ExtractVariables>

 

And as output of that, you have the context variable extracted.severity holding "Sev4". And what you want is just "4".

JSONPath doesn't have a way to express the idea that you want a substring. With JSONPath, You can extract a field or a set of fields, but not a substring of a field. So you must rely on a "post processing" step to get the substring. Probably in Javascript, just use a call to the substring function.

Your logic will be like this:

  1. ExtractVariables with JSONPath (as above)
  2. JavaScript policy that uses substring

The JS code will look something like this

 

var s = context.getVariable('extracted.severity'); // variable set by ExtractVariables policy
s = s.substring(3,4)
context.setVariable('single-digit', s);

You can also get substrings via message templates, in AssignMessage: 

<AssignMessage name='AM-Substring'>
  <AssignVariable>
    <Name>three</Name>
    <Value>3</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>four</Name>
    <Value>4</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>single-digit</Name>
    <Template>{substring(extracted.severity,three,four)}</Template>
  </AssignVariable>
</AssignMessage>

It's weird that you need to use variable names (three, four) for those fields, but that's the way it works today. 

 

Resume back on it. 

Thanks for the solution. It works as expected.

👍

Thanks for the confirmation.