Convert msg.object to a new msg.object (function)

Hallo,

i need an msg-object for the World-Map like this: {"name":"Test", "lat":52.31, "lon":7.05}

i got an msg-object like this:

My idea

var payload=msg.payload;

var newMsg = {"name":"Test", "lat":52.31, "lon":7.05}
newMsg.lat = payload.payload_fields.location.latitude;
newMsg.lon = payload.payload_fields.location.longitude;

return newMsg;

Can someone help?

Greetings

Ernst

Document says:
image

You'll need to think again and reprase the sentence: i need an msg-object for the World-Map like this: {"name":"Test", "lat":52.31, "lon":7.05}

As you see, the payload property of the msg object must be filled with name, lon and lat properties (and their values of course)

I think this works:

var payload=msg.payload;

var newMsg = {payload:{ "name":"Test", "lat":52.31, "lon":7.05}}

newMsg.payload.lat = payload.payload_fields.location.latitude;

newMsg.payload.lon = payload.payload_fields.location.longitude;

return newMsg;

1 Like

You could shrink that slightly - but yes you have the right idea :slight_smile:

var newMsg = {payload:{ "name":"Test"}}
newMsg.payload.lat = msg,payload.payload_fields.location.latitude;
newMsg.payload.lon = msg.payload.payload_fields.location.longitude;
return newMsg;

Thank you!