Hello,
I have the need to take data from the array, msg.payload.devices and reformat it to msg.options as an array of objects. I am trying to use a Template node and mustache template to do so, but am having an issue. When outputting plain text from the template node, the template below appears to get everything I need, save it adds a trailing comma. Changing the output to Parsed JSON gives me an "Unexpected token } in JSON at position 206" error, which is the trailing comma.
How do I either get a mustache template to only include the comma between items, or, what would the syntax be to get me desired output? The ultimate goal is to populate the options in a dashboard's dropdown item.
Current Template adding trailing comma
[{
{{#payload.devices}}
"{{name}}":"{{id}}",
{{/payload.devices}}
}]
Output from above template, as text
[{
"Family Room Chromecast":"0ca5627c5089e7143a6856a8cd5e6abedb11dfce",
"DESKTOP-NUVVO9O":"58460c1d747ba0c7a39afc8f6f56c0d7aff1cb72",
"Squeezebox Touch":"ba7d99ac9fef8b737aa5cd75b86ebdae51689c38",
}]
Desired value of msg.options:
Note lack of trailing comma
[{
"Family Room Chromecast":"0ca5627c5089e7143a6856a8cd5e6abedb11dfce",
"DESKTOP-NUVVO9O":"58460c1d747ba0c7a39afc8f6f56c0d7aff1cb72",
"Squeezebox Touch":"ba7d99ac9fef8b737aa5cd75b86ebdae51689c38"
}]
Value of incoming msg.payload:
{
"devices":[
{
"id":"0ca5627c5089e7143a6856a8cd5e6abedb11dfce",
"name":"Family Room Chromecast",
"type":"TV",
"volume_percent":100
},
{
"id":"58460c1d747ba0c7a39afc8f6f56c0d7aff1cb72",
"name":"DESKTOP-NUVVO9O",
"type":"Computer",
"volume_percent":100
},
{
"id":"ba7d99ac9fef8b737aa5cd75b86ebdae51689c38",
"name":"Squeezebox Touch",
"type":"Speaker",
"volume_percent":49
}
]
}
Nodes to create sample data
[
{
"id": "7ce770e8d2a76610",
"type": "debug",
"z": "bd80cf37.f9982",
"name": "debug 48",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 560,
"y": 1580,
"wires": []
},
{
"id": "e4f58aa89c01fb44",
"type": "inject",
"z": "bd80cf37.f9982",
"name": "",
"props": [
{
"p": "options",
"v": "[{\"Family Room Chromecast\":\"0ca5627c5089e7143a6856a8cd5e6abedb11dfce\",\"DESKTOP-NUVVO9O\":\"58460c1d747ba0c7a39afc8f6f56c0d7aff1cb72\",\"Squeezebox Touch\":\"ba7d99ac9fef8b737aa5cd75b86ebdae51689c38\"}]",
"vt": "json"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 230,
"y": 1540,
"wires": [
[
"53dab3083cf132e4",
"7ce770e8d2a76610"
]
]
},
{
"id": "0a455d4a96bc91ef",
"type": "template",
"z": "bd80cf37.f9982",
"name": "",
"field": "options",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "[{\n{{#payload.devices}}\n \"{{name}}\":\"{{id}}\",\n{{/payload.devices}}\n}]",
"output": "json",
"x": 360,
"y": 1600,
"wires": [
[
"53dab3083cf132e4",
"7ce770e8d2a76610"
]
]
},
{
"id": "bbaec4afeea8bf83",
"type": "inject",
"z": "bd80cf37.f9982",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": "",
"topic": "",
"payload": "{\"devices\":[{\"id\":\"0ca5627c5089e7143a6856a8cd5e6abedb11dfce\",\"is_active\":false,\"is_private_session\":false,\"is_restricted\":false,\"name\":\"Family Room Chromecast\",\"type\":\"TV\",\"volume_percent\":100},{\"id\":\"58460c1d747ba0c7a39afc8f6f56c0d7aff1cb72\",\"is_active\":false,\"is_private_session\":false,\"is_restricted\":false,\"name\":\"DESKTOP-NUVVO9O\",\"type\":\"Computer\",\"volume_percent\":100},{\"id\":\"ba7d99ac9fef8b737aa5cd75b86ebdae51689c38\",\"is_active\":false,\"is_private_session\":false,\"is_restricted\":false,\"name\":\"Squeezebox Touch\",\"type\":\"Speaker\",\"volume_percent\":49}]}",
"payloadType": "json",
"x": 230,
"y": 1600,
"wires": [
[
"0a455d4a96bc91ef"
]
]
}
]