# Using Array in mustache format

**URL:** <https://discourse.nodered.org/t/using-array-in-mustache-format/25050>\
**Category:** General\
**Created:** [18 April 2020 06:02 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050 "2020-04-18T06:02:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![17greesm](https://avatars.discourse-cdn.com/v4/letter/1/848f3c/32.png) [@17greesm](https://discourse.nodered.org/u/17greesm)\
**Post date:** [18 April 2020 06:02 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050/1 "2020-04-18T06:02:03Z")

</div>

Hello guys,  
This is my first post on the forum. I want to print a highchart  
Problem : I am facing issue with use of array in mustache format of template node.  
If my msg.payload = [[0,1],[2,3]]  
this turns {{payload}} into 0,1,2,3 removing any character in payload just leaving comma

 ![8IGoWJ3PHx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b723ca28586286f01c73d43b9a75287c5ee3fe37.png)  
 ![scyqe84jSm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5ec40f48812414caf0915813bc417870b0cb6c21.png)

```auto
[
    {
        "id": "78a6fa27.cac624",
        "type": "function",
        "z": "4d76d926.b15d48",
        "name": "Object to Array",
        "func": "\n\tmsg.payload = [[110, 1],[1, 2],[2, 10]]\n\t\n\t\treturn msg",
        "outputs": 1,
        "noerr": 0,
        "x": 440,
        "y": 1940,
        "wires": [
            [
                "9cab8967.24c438",
                "63c23958.bb1fc8"
            ]
        ]
    },
    {
        "id": "9cab8967.24c438",
        "type": "template",
        "z": "4d76d926.b15d48",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta charset=\"utf-8\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n<script src=\"https://code.highcharts.com/stock/highstock.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/data.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/exporting.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/export-data.js\"></script>\n\n</head>\n<body>\n<script src=\"https://code.highcharts.com/highcharts.js\"></script>\n<div id=\"container\"></div> \n<script>\n \n Highcharts.stockChart('container', {\n\n title: {\n text: 'AAPL Stock Price'\n },\n\n series: [{\n name: 'AAPL',\n // data: {{payload}},\n data: [[0,1],[1,2],[2,10]],\n tooltip: {\n valueDecimals: 2\n }\n }],\n \n rangeSelector: {\n enabled: true\n },\n });\n </script>\n</body>\n</html>\n",
        "output": "str",
        "x": 720,
        "y": 1920,
        "wires": [
            [
                "b254c6a7.1b5818"
            ]
        ]
    },
    {
        "id": "7d99e933.ac36b8",
        "type": "http in",
        "z": "4d76d926.b15d48",
        "name": "",
        "url": "/chart123",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 250,
        "y": 1940,
        "wires": [
            [
                "78a6fa27.cac624"
            ]
        ]
    },
    {
        "id": "b254c6a7.1b5818",
        "type": "http response",
        "z": "4d76d926.b15d48",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 710,
        "y": 2020,
        "wires": []
    },
    {
        "id": "63c23958.bb1fc8",
        "type": "debug",
        "z": "4d76d926.b15d48",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 580,
        "y": 2100,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [18 April 2020 06:45 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050/2 "2020-04-18T06:45:13Z")

</div>

You are outputting as plain text so it will flatten everything including the array to convert it to a string. The same thing happens when you use a `toString()` method in javascript. I’m not sure there is any way to change this behavior. [https://www.w3schools.com/jsref/jsref\_tostring\_array.asp](https://www.w3schools.com/jsref/jsref_tostring_array.asp)  
as at least in javascript this is expected for an array to string conversation.

Johannes

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [18 April 2020 06:58 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050/3 "2020-04-18T06:58:23Z")

</div>

So i think I found a solution:  
You can get around the behavior of an array to string conversion by converting the array to an JSON array string beforehand.  
Just use the `JSON.stringify()` method in the previous function node.

```auto
msg.payload = [[110, 1],[1, 2],[2, 10]];
msg.payload = JSON.stringify(msg.payload);
return msg

```

Which gives you a JSON string representation of the array which in turn doesn’t get flattened by the template node as it’s already a string.

---

<div class="post-metadata">

**Author:** ![17greesm](https://avatars.discourse-cdn.com/v4/letter/1/848f3c/32.png) [@17greesm](https://discourse.nodered.org/u/17greesm)\
**Post date:** [18 April 2020 08:00 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050/5 "2020-04-18T08:00:26Z")

</div>

Thanks for your quick reply. This is now working.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 May 2020 08:00 UTC](https://discourse.nodered.org/t/using-array-in-mustache-format/25050/6 "2020-05-02T08:00:28Z")

</div>

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