Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am getting a payload with some keys that are having values for example

1. mulesoft[04444]
2. muleworld[88990]

now I want to extract content that is enclosed in square brackets here.

let's take an actual problem, here incoming payload

{
    "message": "Hello world[009]",
    "sender" : "myself[001]",
    "to": "friend[0089]"
}

here I want to create a Mulesoft function inside data weave code that I can use to extract value that is enclosed in the square brackets

Please write a DWL code fro this.

question from:https://stackoverflow.com/questions/65900945/how-to-get-a-value-from-a-string-value-that-is-coming-in-a-payload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
770 views
Welcome To Ask or Share your Answers For Others

1 Answer

I faced this issue today in one of the API, so I thought that I should share this with others too. I had written DWL code for its as below

%dw 2.0
import * from dw::core::Strings
output application/json
fun extract(key)= substringBefore((substringAfter(key, "[")),"]")
---
{
    value: extract(payload.message)
}

it gave me desired o/p

{
  "value": "009"
}

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...