Populating dashboard options

There are several posts about this subject but I can't make them work with my data.

Given this input

let locations = [
    {"Camberwell": 
        { 
        "name": "Camberwell",
        "latitude": 51.3, 
        "longitude": -0.5,
        "creation": "2023-05-16T12:34:40.068Z", 
        "lastseen": "2023-05-16T12:34:40.068Z", 
        "borough": "Southwark"
        }
    }, 
    {"Seven Dials": 
       {
        "name": "Seven Dials",
        "latitude": 51.4,
        "longitude": -0.2, 
        "creation": "2023-05-16T12:35:22.813Z", 
        "borough": "Camden" 
        }
}
]

I cannot convert it to pass to a dashboard dropdown as msg.options

[{"Camberwell": "Southwark"}, {"Seven Dials": "Camden"}]

Can someone please show me the way? Either JSONata or function code !

ps the input data could perhaps be created like this, if it makes more sense.

let locations = [
    {
            "name": "Camberwell",
            "latitude": 51.3,
            "longitude": -0.5,
            "creation": "2023-05-16T12:34:40.068Z",
            "lastseen": "2023-05-16T12:34:40.068Z",
            "borough": "Southwark"
    },
    {
            "name": "Seven Dials",
            "latitude": 51.4,
            "longitude": -0.2,
            "creation": "2023-05-16T12:35:22.813Z",
            "borough": "Camden"
    }
]

If the array is in msg.payload then the JSONata would be

$$.payload.{$.*.name: $.*.borough}[]
//or
$$.payload.*.{$.name: $.borough}[]
[{"id":"9222d528a6f09111","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Camberwell\":{\"name\":\"Camberwell\",\"latitude\":51.3,\"longitude\":-0.5,\"creation\":\"2023-05-16T12:34:40.068Z\",\"lastseen\":\"2023-05-16T12:34:40.068Z\",\"borough\":\"Southwark\"}},{\"Seven Dials\":{\"name\":\"Seven Dials\",\"latitude\":51.4,\"longitude\":-0.2,\"creation\":\"2023-05-16T12:35:22.813Z\",\"borough\":\"Camden\"}}]","payloadType":"json","x":90,"y":2580,"wires":[["5bb02cf5ce9ef9fc"]]},{"id":"5bb02cf5ce9ef9fc","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"$$.payload.{$.*.name: $.*.borough}[]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":2580,"wires":[["7731f4e21f67fee5"]]},{"id":"7731f4e21f67fee5","type":"debug","z":"b9860b4b9de8c8da","name":"debug 295","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"options","statusVal":"","statusType":"auto","x":510,"y":2580,"wires":[]}]

[edit] Correct blunder in flow example, set property to options in change node.

Excellent thanks @E1cid. I had to adjust your change node to set msg.options instead of msg.payload but the dropdown works now.

1 Like

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