Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string in dataweave

Tags:

mule

dataweave

I have a string like "Hi I am from "Kazan, Russia". The output should be

Hi
I
am
from
Kazan, Russia

I tried the regex [^\s\"']+|\"([^\"]*)\"|'([^']*)', which works fine on regexp.com. But in dataweave it does nothing. This is what I tried

%dw 1.0
%output application/json
%function split(data) {returnData: data splitBy "[^\s\"']+|\"([^\"]*)\"|'([^']*)'"}
---
split(payload)[0]

Edit 1

I tried scan operator
This is what I did

%dw 1.0
%output application/json
---
{
payload: payload scan /[^\s\"']+|\"([^\"]*)\"|'([^']*)'/
}

But I am getting NullPointerException
Stack trace

Message               : null (java.lang.NullPointerException).
Payload               :      com.mulesoft.weave.reader.DefaultSeekableStream@5d334a28
Element               : /logFlow/processors/0 @ log:log.xml:14 (Transform Message)
Element XML           : <dw:transform-message doc:name="Transform Message" metadata:id="87d4353c-240d-4b1c-84b4-171f6c11045b">
                    <dw:input-payload mimeType="plain/text"></dw:input-payload>
                    <dw:set-payload>%dw 1.0%output application/json---{payload: payload scan /[^\s\"']+|\"([^\"]*)\"|'([^']*)'/}</dw:set-payload>
                    </dw:transform-message>
like image 321
Abhay Avatar asked Apr 09 '26 21:04

Abhay


1 Answers

I am not sure about the syntax you have used in your initial query, but the following should work for the payloads with the following formats:

Hello I am from "Kazan, Russia" 

and:

Hello I am from "Kazan, Russia

The below sample uses:

  1. Possessive Quantifiers that match on the required word characters.

  2. An explicit match on , to help match the location.

  3. Reduce the output from an array.

DataWeave:

%dw 1.0
%input payload application/json
%output application/json
%var data =  "Hello I am from \"StackOverflow, Internet\""
---
data scan /\w++, \w++|\w++/ reduce ($$ ++ $)
like image 115
Matt Jones Avatar answered Apr 13 '26 15:04

Matt Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!