Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

mule - How to get a value from a string value that is coming in a payload,

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...