Split Data from Webhook

How can i split name, email, and phone from text , so that i can process my next node

{

“headers”:{

“host”:“flow.ourklient.com”,

“user-agent”:“tawk.to-webhook”,

“content-length”:“402”,

“content-type”:“application/json”,

“x-forwarded-for”:“35.192.146.36”,

“x-forwarded-host”:“flow.ourklient.com”,

“x-forwarded-port”:“443”,

“x-forwarded-proto”:“https”,

“x-forwarded-server”:“74e9ea293f23”,

“x-hook-event-id”:“7689053242309014”,

“x-real-ip”:“35.192.146.36”,

“x-tawk-signature”:“2a66613edf6bbe464cb9dfeba00031af8a912247”,

“accept-encoding”:“gzip”

},

“params”:{

},

“query”:{

},

“body”:{

“chatId”:“f9b8e850-f09d-11ed-b0e1-d5c591cc9c54”,

“visitor”:{

“name”:“chandan”,

“city”:“delhi”,

“country”:“IN”,

“email”:“wwww”

},

“message”:{

“sender”:{

“type”:“visitor”

},

“text”:“Name : xxxx Phone : 000000 Email : yyyyy”,

“type”:“msg”

},

“time”:“2023-05-12T08:21:35.806Z”,

“event”:“chat:start”,

“property”:{

“id”:“59981cbc1b1bed47ceb057df”,

“name”:“cns”

}

}

}

Hi @chandan988 :wave:

I recorded a quick video on how you can do this. I kept the video generic to make it reusable, but I used your exact example.

The only difference with your example is that your text key might be nested in another data structure. But if you drag and drop it’s key into the expression like in my video, that will all get auto-mapped for you.

2 Likes

[

{

“data”:“Name : edrftgyhujikol Phone : dsfdddd Email : ddddkd”

}

]

I tried But my output is coming like this
{{ $json[“data”].split(" ")[4] }}

After putting split my output is

[Array: [“Name : edrftgyhujikol\r\nPhone : 34567890567\r\nEmail : dddddd”]] in this format

This is because the data in your screenshot is different from the example you shared. Some of the spaces are being translated to linebreak characters (\n ).

If that is happening consistently (i.e. the linebreak chars are always in same place), you could use a combination of split(" ") and "split(\n") to extract the specific infos you need.

If however that varies from input to input, that is sometimes it’s separated by a " " and sometimes by a “/n” … then it becomes a bit more complicated and the first step would be for you to understand if it varies in a predictable way. For example, maybe there are just 2x subcases of variation. If so, my initial thought would be to add an IF node that checks for the specific case (perhaps with regex check) and then routes that item to a set node with an expression to extract that specific format (be it \n or a space)

2 Likes

Another quick thought: if it is only \n that are sometimes replacing spaces (and it’s happening intermittently). Then you could “clean” the text string before you perform the .split(" ").

To do so, we basically need to replace every instance of \n with " " (a space). We do have a .replace() function in n8n and a .replaceAll(), we need the latter :point_down:

.replaceAll("\n", " ") ← that should be the snippet you need to clean the data first.

In full: {{ $json.text.replaceAll("\n", " ").split(" ")[5] }}

That only works if you have 1x “rouge” separator. Though you could chain multiple “replaceAll” functions after each other if you do have a set list of separators you need to clean + swap for a space.

3 Likes

Thanks a lot @maxT for Quick Reply and support

1 Like

My pleasure, glad to hear it helped and thanks for letting the community know that it was a working solution - will help others find this answer in future :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.