Convert buffer into string and then json

Hi,

I'm trying to use NodeRed to parse data coming into NodeRed via a webhook.

When sending a test to NodeRed I get the following as a buffer

[45,45,101,55,100,101,56,56,56,102,45,48,98,51,54,45,52,99,48,50,45,57,100,54,98,45,99,57,48,97,98,98,55,101,101,54,56,51,13,10,67,111,110,116,101,110,116,45,84,121,112,101,58,32,97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110,59,32,99,104,97,114,115,101,116,61,117,116,102,45,56,13,10,67,111,110,116,101,110,116,45,68,105,115,112,111,115,105,116,105,111,110,58,32,102,111,114,109,45,100,97,116,97,59,32,110,97,109,101,61,100,97,116,97,13,10,13,10,123,34,84,105,116,108,101,34,58,34,84,101,115,116,32,78,111,116,105,102,105,99,97,116,105,111,110,34,44,34,68,101,115,99,114,105,112,116,105,111,110,34,58,34,84,101,115,116,32,78,111,116,105,102,105,99,97,116,105,111,110,32,68,101,115,99,114,105,112,116,105,111,110,34,44,34,68,97,116,101,34,58,34,50,48,50,51,45,49,48,45,48,52,84,49,53,58,50,49,58,52,51,46,49,50,52,56,55,55,56,90,34,44,34,69,118,101,110,116,34,58,34,115,121,115,116,101,109,46,119,101,98,104,111,111,107,116,101,115,116,34,44,34,83,101,114,118,101,114,34,58,123,34,78,97,109,101,34,58,34,69,109,98,121,86,49,34,44,34,73,100,34,58,34,57,52,56,102,51,97,98,54,98,99,57,97,52,102,48,100,57,101,97,99,101,52,53,55,56,102,56,102,98,48,98,54,34,44,34,86,101,114,115,105,111,110,34,58,34,52,46,55,46,49,52,46,48,34,125,125,13,10,45,45,101,55,100,101,56,56,56,102,45,48,98,51,54,45,52,99,48,50,45,57,100,54,98,45,99,57,48,97,98,98,55,101,101,54,56,51,45,45,13,10]

image

And the raw version is this

--e7de888f-0b36-4c02-9d6b-c90abb7ee683
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name=data

{"Title":"Test Notification","Description":"Test Notification Description","Date":"2023-10-04T15:21:43.1248778Z","Event":"system.webhooktest","Server":{"Name":"EmbyV1","Id":"948f3ab6bc9a4f0d9eace4578f8fb0b6","Version":"4.7.14.0"}}
--e7de888f-0b36-4c02-9d6b-c90abb7ee683--

But I'd like to convert it into a string (to get rid of the top 4 lines) and then convert to JSON

I've tried using node-red-contrib-buffer-parser 3.22 but honestly I'm lost as to which options that I need.

Test flow export

[{"id":"7d8a97b45e1b69c9","type":"inject","z":"b1dcf7684deceb7b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[45,45,101,55,100,101,56,56,56,102,45,48,98,51,54,45,52,99,48,50,45,57,100,54,98,45,99,57,48,97,98,98,55,101,101,54,56,51,13,10,67,111,110,116,101,110,116,45,84,121,112,101,58,32,97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110,59,32,99,104,97,114,115,101,116,61,117,116,102,45,56,13,10,67,111,110,116,101,110,116,45,68,105,115,112,111,115,105,116,105,111,110,58,32,102,111,114,109,45,100,97,116,97,59,32,110,97,109,101,61,100,97,116,97,13,10,13,10,123,34,84,105,116,108,101,34,58,34,84,101,115,116,32,78,111,116,105,102,105,99,97,116,105,111,110,34,44,34,68,101,115,99,114,105,112,116,105,111,110,34,58,34,84,101,115,116,32,78,111,116,105,102,105,99,97,116,105,111,110,32,68,101,115,99,114,105,112,116,105,111,110,34,44,34,68,97,116,101,34,58,34,50,48,50,51,45,49,48,45,48,52,84,49,53,58,50,49,58,52,51,46,49,50,52,56,55,55,56,90,34,44,34,69,118,101,110,116,34,58,34,115,121,115,116,101,109,46,119,101,98,104,111,111,107,116,101,115,116,34,44,34,83,101,114,118,101,114,34,58,123,34,78,97,109,101,34,58,34,69,109,98,121,86,49,34,44,34,73,100,34,58,34,57,52,56,102,51,97,98,54,98,99,57,97,52,102,48,100,57,101,97,99,101,52,53,55,56,102,56,102,98,48,98,54,34,44,34,86,101,114,115,105,111,110,34,58,34,52,46,55,46,49,52,46,48,34,125,125,13,10,45,45,101,55,100,101,56,56,56,102,45,48,98,51,54,45,52,99,48,50,45,57,100,54,98,45,99,57,48,97,98,98,55,101,101,54,56,51,45,45,13,10]","payloadType":"bin","x":170,"y":160,"wires":[["e29b35f75a4ebbe8"]]},{"id":"e29b35f75a4ebbe8","type":"debug","z":"b1dcf7684deceb7b","name":"debug 395","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":390,"y":160,"wires":[]}]

That node is more for extracting bits and bytes and number conversions (though it can convert the data to a string for you - it is just overkill).

You need simple buffer->string and a JSON node.

In a function, you can do something like

const str = msg.payload.toString() // convert buffer to string
const split = str.split('\r\n') // convert to an array of lines
msg.payload = split[4] // pick the 4th line
return msg

untested

so long as msg.payload IS a buffer

then pass the msg through a JSON node to make a pretty/usable object.

NOTE: It would be far better is the webhook only sent the data in the payload. Are you using built in the HTTP-in node for intercepting the webhook?

This works perfectly, thanks.

const split = str.split('\r\n') // convert to an array of lines
msg.payload = split[4] // pick the 4th line
return msg

It would be far better is the webhook only sent the data in the payload

Agreed

Are you using built in the HTTP-in node for intercepting the webhook?

Yup

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