Switch node - jsonata - magic?

Awesome, thanks! It could open up a can of positive worms :smiley:

I have a follow-up question relating to this. I've tried to changed @shrickus example flow above for the message below in which there are keys in the payload.parametrar array that appear in different positions, which are a bit difficult to catch with conditions in, say, a function node. I've also experimented with jsonata expressions in a switch node but with no success.

{"index":[7],"timme":-5,"timmenu":17,"tid":"2019-08-08T12:00:00Z","datum":"8/8 kl.12","plats":[15.611086,60.599615],"parametrar":[{"name":"msl","levelType":"hmsl","level":0,"unit":"hPa","values":[1001.1]},{"name":"t","levelType":"hl","level":2,"unit":"Cel","values":[16.4]},{"name":"vis","levelType":"hl","level":2,"unit":"km","values":[22.5]},{"name":"wd","levelType":"hl","level":10,"unit":"degree","values":[114]},{"name":"ws","levelType":"hl","level":10,"unit":"m/s","values":[3.6]},{"name":"r","levelType":"hl","level":2,"unit":"percent","values":[94]},{"name":"tstm","levelType":"hl","level":0,"unit":"percent","values":[12]},{"name":"tcc_mean","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"lcc_mean","levelType":"hl","level":0,"unit":"octas","values":[7]},{"name":"mcc_mean","levelType":"hl","level":0,"unit":"octas","values":[2]},{"name":"hcc_mean","levelType":"hl","level":0,"unit":"octas","values":[7]},{"name":"gust","levelType":"hl","level":10,"unit":"m/s","values":[8.6]},{"name":"pmin","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.6]},{"name":"pmax","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.9]},{"name":"spp","levelType":"hl","level":0,"unit":"percent","values":[0]},{"name":"pcat","levelType":"hl","level":0,"unit":"category","values":[3]},{"name":"pmean","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.8]},{"name":"pmedian","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.8]},{"name":"Wsymb2","levelType":"hl","level":0,"unit":"category","values":[20]}]}

Say, for example, I'm interested in "values" of payload.parametrar.name = "t" but that "t" appears in different positions in the array. How could I use a switch node with jsonata to find it? Are there better ways than using jsonata?

Here is how I would use a change node to find a field with the name "t", returning its value(s):

And yes, I think JSONata is the right tool for this job...

Here is the flow I used to test the expression:

[{"id":"f4a98652.0d5998","type":"inject","z":"58c8eb7a.5496c4","name":"sample data","topic":"","payload":"{\"index\":[7],\"timme\":-5,\"timmenu\":17,\"tid\":\"2019-08-08T12:00:00Z\",\"datum\":\"8/8 kl.12\",\"plats\":[15.611086,60.599615],\"parametrar\":[{\"name\":\"msl\",\"levelType\":\"hmsl\",\"level\":0,\"unit\":\"hPa\",\"values\":[1001.1]},{\"name\":\"t\",\"levelType\":\"hl\",\"level\":2,\"unit\":\"Cel\",\"values\":[16.4]},{\"name\":\"vis\",\"levelType\":\"hl\",\"level\":2,\"unit\":\"km\",\"values\":[22.5]},{\"name\":\"wd\",\"levelType\":\"hl\",\"level\":10,\"unit\":\"degree\",\"values\":[114]},{\"name\":\"ws\",\"levelType\":\"hl\",\"level\":10,\"unit\":\"m/s\",\"values\":[3.6]},{\"name\":\"r\",\"levelType\":\"hl\",\"level\":2,\"unit\":\"percent\",\"values\":[94]},{\"name\":\"tstm\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"percent\",\"values\":[12]},{\"name\":\"tcc_mean\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"octas\",\"values\":[8]},{\"name\":\"lcc_mean\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"octas\",\"values\":[7]},{\"name\":\"mcc_mean\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"octas\",\"values\":[2]},{\"name\":\"hcc_mean\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"octas\",\"values\":[7]},{\"name\":\"gust\",\"levelType\":\"hl\",\"level\":10,\"unit\":\"m/s\",\"values\":[8.6]},{\"name\":\"pmin\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"kg/m2/h\",\"values\":[0.6]},{\"name\":\"pmax\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"kg/m2/h\",\"values\":[0.9]},{\"name\":\"spp\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"percent\",\"values\":[0]},{\"name\":\"pcat\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"category\",\"values\":[3]},{\"name\":\"pmean\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"kg/m2/h\",\"values\":[0.8]},{\"name\":\"pmedian\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"kg/m2/h\",\"values\":[0.8]},{\"name\":\"Wsymb2\",\"levelType\":\"hl\",\"level\":0,\"unit\":\"category\",\"values\":[20]}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":4360,"wires":[["5187694e.d72ec8"]]},{"id":"5187694e.d72ec8","type":"change","z":"58c8eb7a.5496c4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.**[name=\"t\"].values","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":4360,"wires":[["508d8ae6.100244"]]},{"id":"508d8ae6.100244","type":"debug","z":"58c8eb7a.5496c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":4360,"wires":[]}]
1 Like

That's brilliant, thanks a lot!

The code below is exactly what I needed! One question though, is it possible that all the messages go through 1 output one by one instead that each msg has his own output? I have a variable amount of objects in an array. So would be a lot of work to create an output for eacht object. And sometimes I have 1 object in an array. How can I prevent the function for splitting up 1 object?

my goal is that each object (with 4 payloads) will get seperated

var keys = Object.keys(msg.payload);
var msgs = [];

for (var ii = 0; ii < keys.length; ii++) {
    var key = keys[ii];
    msgs[ii] = {
        "topic": "output-" + ii,
        "payload": msg.payload[key]
    }
}

return msgs;

You're now returning an array of objects in the shape of

[
    {
         "topic": "output-...",
         "payload": ...
    },
    ...
]

For a function node, if you return an array, each of these will be send to their own output on a function. To send those as separate messages to the same output, you wrap your array in another array. That way, the first element of the outer array goes to the first output, where they'll come out one by one:

[
    [
        {
             "topic": "output-...",
             "payload": ...
        },
       ...
    ]
]

I hope that answers your question.

1 Like

Ah yes, got it!

Sometimes I only have 1 object in my array, something like below, the function that was shared here will also split this object to title, description etc. So the individual keys will split. How can I prevent that from happening when there is just one object? When I have an array with two objects only the complete object will get seperated which Is wat Im aiming for
object
title: "lorum ipsum"
description: "lorum ipsum"
url: "lorum ipsuml"
id: "lorum ipsum"