user13747070
user13747070

Reputation:

Logic App - Expression to get a particular substring from a string

I have below JSON input in my Logic App:

{
   "d":{
      "results":[
         {
            "userId":"123",
            "payGrade":"KF15-314-370-AGR342-PD1"
         },
         {
            "userId":"456",
            "payGrade":"KF14-269-313-AGR291-R4"
         },
         {
            "userId":"789",
            "payGrade":"KF14-269-313-AGR289"
         }
      ]
   }
}

I want to use only a last part/substring after "AGR-" from "payGrade" (i.e. "PD1" from "KF15-314-370-AGR342-PD1" | "R4" from "KF14-269-313-AGR291-R4" | and if there's nothing after "AGR" just make it blank)

I've been looking at "substring('text', startIndex, length)" expression function but it only returns substring from a particular position/index so not sure how to achieve this.

Upvotes: 0

Views: 12038

Answers (1)

PKiong
PKiong

Reputation: 551

There are two ways of doing this,

Short way (I do not recommend this, as the code itself is nested, make sure there is a loop on results array)

if(equals(length(split(items('For_each')?['payGrade'],'-')),5), last(split(items('For_each')?['payGrade'],'-')),'')

Long answer (Recommended, it is much concise and easy to follow)

You can follow the logic in the image below

  1. Make an empty array variable (Used to return, since u are looping in array and can't really return any result from it, modify as needed)

  2. Loop through the results array

  3. Split the string using "-" @{split(items('For_each')?['payGrade'],'-')}

  4. Condition to check whether the result from split is 4 or 5 item

You should get 5 items if there is payGrade, and 4 if there is no payGrade

  1. Take the value accordingly and append it to the variable on the outer scope

  2. Do whatever you want with that Variable.

enter image description here

Upvotes: 0

Related Questions