I want to convert string data into json structure

I am receving an input from esp32 serially as shown below:
geodata:18.34-64.68;device_ID:1;Temperature:10 degC;Humidity:2 RH;WindSpeed:5 m/s
geodata:15.34-34.67;device_ID:3;Temperature:10 degC;Humidity:2 RH;WindSpeed:5 m/s

I receive this 2 data together.
my logic is: For each device, geodata will be fixed. So I want to build 2 objects from this string in the below structure:
{
"geodata": "15.34-34.67",
"device_ID": 3,
"wind": {
"ts": [
1,
2,
3,
4,
5,
6
],
"Temperature": [
1.1,
2.2,
3.5,
4.5,
5.4,
6.5
],
"Humidity": [
1.1,
2.2,
3.5,
4.5,
5.4,
6.5
],
"windspeed": [
1.1,
2.2,
3.5,
4.5,
5.4,
6.5
],
}
}

Earlier I was using the below code when I was receiving just one string "geodata:18.34-64.68;device_ID:1;Temperature:10 degC;Humidity:2 RH;WindSpeed:5 m/s" as an input:

const i = msg.payload.split(";")
const geodata = i[1].split(":")[1];
const device_ID = Number(i[0].split(":")[1]);
const Temperature = i[2].split(":")[1];
const Humidity = i[3].split(":")[1].trim();
// const WindSpeed = i[4].split(":")[1];
const now = new Date()
let mydata = context.get("mydata") ?? {}
if (mydata.hasOwnProperty("geodata")) {
}
else {
mydata.geodata = geodata
mydata.deviceid = device_ID
mydata.timestamp =
mydata.temperature =
mydata.humidity =
//mydata.windspeed =
}
mydata.timestamp.push(now)
mydata.temperature.push(Temperature)
mydata.humidity.push(Humidity)
//mydata.windspeed.push(WindSpeed)
if (mydata.temperature.length == 10) {
msg.payload = mydata
context.set("mydata", {})
return msg;
} else {
context.set("mydata", mydata)
return null;
}

And yet another one. That is the third almost identical post like this today. What an amazing co-incidence.

Well there is a Dhanush Manya Hosamane who is "Pursuing Master's of Engineering in Automation and IT at Technische Hochschule Köln." after "3 years of experience as a Control system engineer at Yokogawa India Limited"

And there is a Yash Pisat who is "pursuing a Master's degree in Automation & Industrial IT at the Technical University of Cologne, with a focus in Data Science, Analytics, and Engineering." after having worked on "machine learning and deep learning algorithms, as well as optimization techniques, to give solutions to real-world problems."

And then there is a Sneha Hirwal who is connected with " Masters of Engineering in Automation and IT(Data Science) at Technische Hochschule Köln" who says she would "want to put my expertise to good use by providing relevant and meaningful insights."

I don't think that these people can be the same as @YashPisat , @snehakhirwal02 and @dhanushmh though because three such intelligent and motivated IT students would have more pride than to try and get other people to do their homework, or at least the wit to ask ChatGPT first.

2 Likes

Yeah unfortunately we are trying to do it by ourselves, but we aren't asking people to do our homework. Trying to ask doubts or errors what I was facing, I think that's what the forum is used for. Sorry if it's spam, I would ask my colleagues to delete if there is an option to do it. Thank you for the support.

No one was trying hard to hide, their names were clearly searchable.

In the interest of learning and openness and sharing

Try this

const lines = msg.payload.split(/\n/);
const output = {};
let hold = {};
lines.forEach(line => {
    line.split(";").forEach(keyval => {
        keyval = keyval.split(":");
        hold[keyval[0]] = keyval[1]
    });
    if(!output[hold.device_ID]){
        output[hold.device_ID] = {
            geodata: hold.geodata, 
            device_ID: hold.device_ID,
            wind: {
                ts:[],
                temperature:[],
                humidity:[],
                windspeed:[],
                units: {
                    temperature: hold.Temperature.split(" ")[1],
                    humidity:hold.Humidity.split(" ")[1],
                    windspeed:hold.WindSpeed.split(" ")[1]
                }
            }
        }
    }
    output[hold.device_ID].wind.ts.push(new Date());
    output[hold.device_ID].wind.temperature.push(parseFloat(hold.Temperature));
    output[hold.device_ID].wind.humidity.push(parseFloat(hold.Humidity));
    output[hold.device_ID].wind.windspeed.push(parseFloat(hold.WindSpeed));
    hold = {}    
})
msg.payload = output
return msg

should accept multiple lines and repeated deice_ID's it also add a units and parses the numeric numbers

Hope it at least teaches you something.

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