excuse my lack of JavaScript, is there some docs or could you explain what happens here?
for (let key in msg.payload){ // the JSON object is looped on the let-variable "key" (in the test data [0], [1], [3])
let msg1 = {payload:{}}; // variable msg1 is initialized as JSON object with an empty "payload"
msg1.payload[key] = Object.assign(msg.payload[key],{"key":key}); // msg1.payload is filled with the "key" node and subsequent nodes
node.send(msg1) // JSON object msg1 is send
}
return;
So now, the "change" node in node-red gets three msg1.payloads containg the subsequent nodes, correct?
$sift(payload.*, function($v,$k){$k in ["key","identifier","sender","sent","status"]})
sift now returns all nodes matching value/key pairs, the function finds - and fills "msg.payload" with it. So now we have a JSON containing only the found keys of the function, correct?
for (let key in msg.payload){ // the JSON object is looped on the let-variable "key" (now only the defined attributes)
if (key !== "key"){ // do nothing for the key-identifier
let msg1 = {payload:{}}; // initialize msg1 as empty JSON object
msg1.payload = msg.payload[key]; // fill the msg1.payload with the current "key"-value
msg1.topic = "my/mqtt/topic/" + key + msg.payload.key; // create the mqtt topic with the "key"-name
node.send(msg1); // send the message - using the mqtt node I suppose
}
}
return
so finally, the function iterates the sent msg.payload for the JSON and sends mqtt-messages to the created topics.
I think I got this right...
What if I want to straighten the JSON even more, like the "real" information come subsequent of the node "info" (which I shortened initially):
{
"0": {
"identifier": "DE-BY-A-W083-20200828-000",
"sender": "DE-BY-A-W083",
"sent": "2020-08-28T11:00:08+02:00",
"status": "Actual",
"msgType": "Update",
"scope": "Public",
"code": [
"1.0",
"nina"
],
"references": "DE-BY-A-W083-20200317-000 DE-BY-A-W083-20191213-000 DE-BY-A-W083,DE-BY-A-W083-20200317-000 DE-BY-A-W083-20191213-000 DE-BY-A-W083-20190821-000,2019-08-21T00:00:00+00:00",
"info": [
{
"language": "DE",
"category": [
"Health",
"Other"
],
"event": "Gefahreninformation",
"urgency": "Immediate",
"severity": "Minor",
"certainty": "Observed",
"eventCode": [
Would I search for the subsequent keys in the first function to create a "flat" JSON with only my information (like [0].info.[0].certainty for example => $certainty), or would I do that in the last function just before I send the mqtt messages?
...and how would this be done ideally? This time I guess it would be easiest to pick directly the "deep linked" attributes, which are of concern and not just straight out "flatten" the whole JSON. I'm a bit stuck in how to start to do this.
I did incluce "info" in the change-node, and then I added another function directly afterwards:
msg.payload["language"] = msg.payload["info"][0]["language"]
delete msg.payload["info"];
return msg;
Is this a efficient way to deal with that? like this:
[{"id":"47dc9260.3223cc","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"9a4335c6.75c7e8","type":"inject","z":"47dc9260.3223cc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"0\":{\"identifier\":\"DE-BY-A-W083-20200828-000\",\"sender\":\"DE-BY-A-W083\",\"sent\":\"2020-08-28T11:00:08+02:00\",\"status\":\"Actual\",\"msgType\":\"Update\",\"scope\":\"Public\",\"code\":[\"1.0\",\"nina\"],\"info\":[{\"language\":\"DE\",\"category\":[\"Health\",\"Other\"],\"event\":\"Gefahreninformation\",\"urgency\":\"Immediate\",\"severity\":\"Minor\",\"certainty\":\"Observed\"}]},\"1\":{\"identifier\":\"DE-NW-BN-SE030-20201014-30-000\",\"sender\":\"DE-NW-BN-SE030\",\"sent\":\"2020-10-14T16:35:21+02:00\",\"status\":\"Actual\",\"msgType\":\"Update\",\"scope\":\"Public\",\"code\":[\"1.0\",\"medien_ueberregional\",\"nina\"],\"info\":[{\"language\":\"DE\",\"category\":[\"Health\",\"Other\"],\"event\":\"Gefahreninformation\",\"urgency\":\"Immediate\",\"severity\":\"Minor\",\"certainty\":\"Observed\"}]},\"2\":{\"identifier\":\"DE-BY-M-W060-20201209-000\",\"sender\":\"DE-BY-M-W060\",\"sent\":\"2020-12-09T14:35:17+01:00\",\"status\":\"Actual\",\"msgType\":\"Alert\",\"scope\":\"Public\",\"code\":[\"1.0\"],\"info\":[{\"language\":\"DE\",\"category\":[\"Health\",\"Other\"],\"event\":\"Gefahreninformation\",\"urgency\":\"Immediate\",\"severity\":\"Minor\",\"certainty\":\"Observed\"}]}}","payloadType":"json","x":140,"y":140,"wires":[["46610142.50118"]]},{"id":"46610142.50118","type":"function","z":"47dc9260.3223cc","name":"","func":"for (let key in msg.payload){\n let msg1 = {payload:{}};\n msg1.payload[key] = Object.assign(msg.payload[key],{\"key\":key});\n node.send(msg1)\n}\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":200,"wires":[["10738584.23f9a2"]]},{"id":"10738584.23f9a2","type":"change","z":"47dc9260.3223cc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sift(payload.*, function($v,$k){$k in [\"key\",\"identifier\",\"sender\",\"sent\",\"status\", \"info\"]})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":140,"wires":[["8bb93548.e088e","d70f99b4.107078"]]},{"id":"b7341d97.470e78","type":"function","z":"47dc9260.3223cc","name":"","func":"for (let key in msg.payload){\n if (key !== \"key\"){\n let msg1 = {payload:{}};\n msg1.payload = msg.payload[key];\n msg1.topic = \"my/mqtt/topic/\" + key + msg.payload.key;\n node.send(msg1)\n }\n}\nreturn","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":240,"wires":[["ec76a518.e86c88","2e7fd233.af7bae"]]},{"id":"ec76a518.e86c88","type":"mqtt out","z":"47dc9260.3223cc","name":"","topic":"","qos":"2","retain":"false","broker":"35ccc936.fc2256","x":1150,"y":220,"wires":[]},{"id":"92cdcc2d.366b08","type":"mqtt in","z":"47dc9260.3223cc","name":"","topic":"my/mqtt/topic/#","qos":"2","datatype":"auto","broker":"35ccc936.fc2256","x":260,"y":480,"wires":[["a2bea5e6.17f7c8"]]},{"id":"a2bea5e6.17f7c8","type":"debug","z":"47dc9260.3223cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":480,"wires":[]},{"id":"2e7fd233.af7bae","type":"debug","z":"47dc9260.3223cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":60,"wires":[]},{"id":"8bb93548.e088e","type":"debug","z":"47dc9260.3223cc","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":60,"wires":[]},{"id":"d70f99b4.107078","type":"function","z":"47dc9260.3223cc","name":"","func":"msg.payload[\"language\"] = msg.payload[\"info\"][0][\"language\"]\ndelete msg.payload[\"info\"]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":240,"wires":[["b7341d97.470e78"]]},{"id":"35ccc936.fc2256","type":"mqtt-broker","z":null,"name":"free","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]