Extracting & Appending Data

Hi,
I’m trying to extract a chunk of text from my payload and append some text to it. Basically, it is a certificate file that I’m reading using a modem using AT command. I’ll be extracting the contents of the certificate in 4 batches using AT command.

My response for each AT command is something like this:
+CSIM: 201,"90CC715D02E87B9BF513....."

I only want to extract the part within the quotes. +CSIM:201 is actually the response to the command and the text length.

I’m trying to create a single file with all 4 outputs of AT command and append text to it. So my final output should look something like this:

-----BEGIN CERTIFICATE-----
MIIDWTCCAkGgAwIBAgIUfsYH/vNNh3zb5bBUdqBQe3Z+izEwDQYJKoZIhvcNAQEL
BQAwTTFLMEkGA1UECwxCQW1

-----END CERTIFICATE-----

How can I do this with nodered? I can handle the AT command part, I need help on extracting the contents of the file within the quotes, combining 4 outputs into a single file, append BEGIN CERTIFICATE and END CERTIFICATE on top and bottom of the extracted text.

If you pass in a message with that string in the payload then you can extract the bit in quotes using
var part = msg.payload.split("\"")[1];

It is not clear exactly what you want to do with that. Do you mean there well be four such messages and you want to combine them into one long string which is the certificate? If so is the particular part determinable somehow? Or is it just the order?

Thanks. I’m not sure is it because I’m trying to simulate my use case with inject node, I get my output same as the input with your suggestion. I have an inject node with string selected on it and the payload is +CSIM: 5,“12345”. I want to extract only 12345 from this. However, since it is a string injection, the entire payload is getting quoted I guess.

Sorry if my earlier message was not clear enough. I will have 4 sets of such messages and I want to combine the output into a single string which is the certificate. At the moment, I’m not concerned about the order. I will have some logic to get them in order once I figure out the above.

I found the following node-red-contrib-string and am playing with it to extract the contents within the quotes.

It works for me

[{"id":"3fbda206.9153fe","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"+CSIM: 201,\"90CC715D02E87B9BF513.....\"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":115,"y":1197,"wires":[["eeeb6825.2025b8"]]},{"id":"eeeb6825.2025b8","type":"function","z":"6dc690a3.1abc88","name":"Extract bit in quotes","func":"var part = msg.payload.split(\"\\\"\")[1];\nmsg.payload = part;\nreturn msg;","outputs":1,"noerr":0,"x":318.5,"y":1196,"wires":[["c098c511.04cb4"]]},{"id":"c098c511.04cb4","type":"debug","z":"6dc690a3.1abc88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":549,"y":1195,"wires":[]}]

Works great now! Thanks @Colin. Is there a way I can convert the payload into HEX? Coz the certificate needs to be in Base64. The format I posted above is actually a HEX, but when it is sent through Inject node, I’m not getting the same Base64 output I get when I convert a HEX to Base64.

For instance, in +CSIM: 10, “4142434445”, the “4142434445” is actually a HEX value. When I extract it, I’m assuming it becomes a string/ ascii value. Therefore, when I use the Base64 node, I get a different value of Base64 conversion output.

@Colin, I managed to get the required output with the following. Extracted the portion within the quotes, I had to remove last 4 characters of the payload, so used slice to remove it, then used Buffer to transform the payload to the correct format. With this, I can get the desired output. Let me know if I can improve something in my approach.

var part = msg.payload.split("\"")[1];
msg.payload = part;

var myString = msg.payload;
var sillyString = myString.slice(0, -4);

msg.payload = Buffer.from(sillyString, "hex");
return msg;

If you use the string split() function to extract the string “4142434445” out of your data, it’s still the same string of numbers, but without the prefix and quotes…

In your case, you need to convert it into the ascii characters represented by the hex digits, taken 2 at a time from the string (i.e. “ABCDE”): In a function node, you can try this javascript code:

var hexes = msg.payload.split('"')[1];
var ascii = new Buffer(hexes, 'hex').toString();