I am trying to convert a json payload that has a series of GPS location points and convert this payload into a GPX file format. The json payload is formatted as an Object of Objects. For this I need to loop through the Object of Objects and get specific values from it to put in the new GPX file format. However, I can't find the correct method to do this.
Format op json payload:
{
"1": {
"name": "msg_ublox_location",
"data": {
"latitude": 52.167908,
"longitude": 5.129468,
"altitude": 56.758,
"success": true,
"hot_retry": 0,
"cold_retry": 0,
"ttf": 0,
"fix_type": 3,
"siv": 9,
"h_acc_est": 2,
"pDOP": 2,
"fix_time": 1636552316,
"active_tracking": true,
"cog": 18000,
"sog": 0
},
"port": 2,
"timestamp": 1636552317,
"timestamp_parsed": "2021-11-10 14:51:57"
},
"2": {
"name": "msg_ublox_location",
"data": {
"latitude": 52.167704,
"longitude": 5.136292,
"altitude": 50.814,
"success": true,
"hot_retry": 0,
"cold_retry": 0,
"ttf": 0,
"fix_type": 3,
"siv": 9,
"h_acc_est": 3,
"pDOP": 1,
"fix_time": 1636552381,
"active_tracking": true,
"cog": 18000,
"sog": 0
},
"port": 2,
"timestamp": 1636552381,
"timestamp_parsed": "2021-11-10 14:53:01"
}
}
The output format I need to convert it to is GPX, and should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0
http://www.topografix.com/GPX/1/0/gpx.xsd">
<name>gpx file name</name>
<trk><name>gpx track name</name><number>1</number><trkseg>
<trkpt lat="52.167908" lon="5.129468"><ele>"56.758"</ele><time>"2021-11-10T13:51:56.000Z"</time></trkpt>
<trkpt lat="52.167704" lon="5.136292"><ele>"50.814"</ele><time>"2021-11-10T13:53:01.000Z"</time></trkpt>
</trkseg></trk>
</gpx>
I have tried to use the following function node, but it does not work:
var tracks_msgs = msg.payload;
var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<gpx version=\"1.0\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns=\"http://www.topografix.com/GPX/1/0\"\n" +
" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0\n" +
" http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"+
" <name>gpx file name</name>\n" +
" <trk><name>gpx track name</name><number>1</number><trkseg>\n";
for (var i = 0; i < tracks_msgs.length; i++) {
xml += " <trkpt lat=\""+ tracks_msgs[i].data.latitude +
"\" lon=\"" + tracks_msgs[i].data.longitude +
"\"><ele>" + tracks_msgs[i].data.altitude + "</ele>" +
"<time>" + new Date(tracks_msgs[i].data.fix_time*1000).toISOString() + "</time></trkpt>\n";
}
xml += " </trkseg></trk>\n</gpx>\n";
msg.payload = xml;
return msg;
Can someone show me how to adapt this this function?