I am new to Node Red and I want to parse the data coming in via serial port, and convert it to JSON format.
The data string I'm getting is from a demo board, and it arrives every one second as follows:
let text = msg.payload.slice(1, -1) //remove outer |
text = text.split("|") //use | as separator
let data = {
"hum": "value",
"temp": "value",
"pres": "value",
"temp_2": "value",
"acc": "value",
"gyr": "value",
"acc_2": "value",
"mag": "value",
}
const key=Object.keys(data) // list of keys
for (let index = 0; index < text.length; index++) { //iterate over text
let element = text[index].split(":"); // split out the value
data[key[index]] = element[1].trim() //set required key to value without exta spaces
}
msg.payload= data
return msg;