# Convert string value to something to work with

**URL:** <https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434>\
**Category:** General\
**Created:** [4 June 2022 02:18 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434 "2022-06-04T02:18:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 02:18 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/1 "2022-06-04T02:18:35Z")

</div>

Hello All,

With help from the community on here I managed to get my project up and working. Then unfortunately I have had to change the way the data comes into Node Red.

Somehow I need to split the values in the string and covert to numbers so I can work with then in a function node.

I've been trying substring without much joy.

Heres how they are coming in now from Mosquito mqtt node -

![Screen Shot 2022-06-04 at 02.54.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f911008459b0f3c954bbf7a9391006ce1d89910f.png)

Most things I try result in - unexpected token in JSON position blah blah or empty payload?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 03:30 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/2 "2022-06-04T03:30:00Z")

</div>

Just change the payload from a string to an array with a comma as separator and at least starting with the second element, each value can easily converted nto a number, using the $number function

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 03:40 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/3 "2022-06-04T03:40:35Z")

</div>

Thank you very much for your reply. I'm not the greatest at Node Red. How would you go about doing this please? I've had a few tries but I cant get it to work.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 03:47 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/4 "2022-06-04T03:47:50Z")

</div>

Copy the payload string here, then I can do a flow for you. But as screenshot I have to type your string manually.

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 03:49 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/5 "2022-06-04T03:49:46Z")

</div>

Thank you very much.

2022/06/04 04:49:19,1,0.066,-0.16,2,22.2883,21.8716,3,1.238,21.0,0.498,101.95,4,1818.99,21.0,0.0

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 04:00 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/6 "2022-06-04T04:00:54Z")

</div>

I was trying this var output = msg.payload.split(","); in a function node, but didn't seem to work.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 04:14 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/7 "2022-06-04T04:14:22Z")

</div>

Using your string - you can do something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b041e1c44cc98d907d1ddcb48ef379e5417e00e.png)

or instead of join - you could also directly access the values:

```auto
[
    {
        "id": "b58dd0fccc7f187b",
        "type": "switch",
        "z": "5b209b6d5006b849",
        "name": "",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "gt",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": true,
        "outputs": 2,
        "x": 470,
        "y": 500,
        "wires": [
            [
                "695bea8f95690585"
            ],
            [
                "bcca667b44d996d5"
            ]
        ]
    },
    {
        "id": "5ef6e01d5e727c8d",
        "type": "inject",
        "z": "5b209b6d5006b849",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2022/06/04 04:49:19,1,0.066,-0.16,2,22.2883,21.8716,3,1.238,21.0,0.498,101.95,4,1818.99,21.0,0.0",
        "payloadType": "str",
        "x": 170,
        "y": 500,
        "wires": [
            [
                "4b7c3669c59df552"
            ]
        ]
    },
    {
        "id": "4b7c3669c59df552",
        "type": "split",
        "z": "5b209b6d5006b849",
        "name": "",
        "splt": ",",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 330,
        "y": 500,
        "wires": [
            [
                "b58dd0fccc7f187b"
            ]
        ]
    },
    {
        "id": "bcca667b44d996d5",
        "type": "change",
        "z": "5b209b6d5006b849",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$number($trim(payload))\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "values",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 670,
        "y": 520,
        "wires": [
            [
                "28980d0cccce3e3a"
            ]
        ]
    },
    {
        "id": "8fe60570c7c237ca",
        "type": "debug",
        "z": "5b209b6d5006b849",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1130,
        "y": 480,
        "wires": []
    },
    {
        "id": "28980d0cccce3e3a",
        "type": "join",
        "z": "5b209b6d5006b849",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "1",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 850,
        "y": 520,
        "wires": [
            [
                "6d6cd0183b92c966"
            ]
        ]
    },
    {
        "id": "695bea8f95690585",
        "type": "change",
        "z": "5b209b6d5006b849",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "date",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 660,
        "y": 480,
        "wires": [
            [
                "6d6cd0183b92c966"
            ]
        ]
    },
    {
        "id": "6d6cd0183b92c966",
        "type": "join",
        "z": "5b209b6d5006b849",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 990,
        "y": 480,
        "wires": [
            [
                "8fe60570c7c237ca"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 04:21 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/8 "2022-06-04T04:21:01Z")

</div>

Just to split the string into an array - you do not need a function node as well - a change node already does this job - if you dont want to use a split node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/449cacc751632d039dcdb9139df277af6f2492fc.png)

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 04:21 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/9 "2022-06-04T04:21:27Z")

</div>

Thank you very much. For some reason I cannot import the flow?

 ![Screen Shot 2022-06-04 at 05.20.33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32ac090d48fdd508f5dc382895d375f7f9d5825b.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 04:22 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/10 "2022-06-04T04:22:53Z")

</div>

Try again - i removed the chars at beginning and end.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [4 June 2022 04:30 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/11 "2022-06-04T04:30:23Z")

</div>

Even a function node does work to convert into an array - but as function nodes are nontransparent - I avoid to code.

```auto
msg.payload = msg.payload.split(",");
return msg;

```

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [4 June 2022 04:33 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/12 "2022-06-04T04:33:58Z")

</div>

Excellent. That works!

Thank you very much for helping.

Now just to change my function node and should get it all graphing!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 June 2022 05:46 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/13 "2022-06-04T05:46:23Z")

</div>

If you simply pass that through a csv node you should get the values as numbers. You can even name the object properties.

---

<div class="post-metadata">

**Author:** ![btbr](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@btbr](https://discourse.nodered.org/u/btbr)\
**Post date:** [12 June 2022 01:21 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/14 "2022-06-12T01:21:04Z")

</div>

Nice one Steve. Worked perfectly.

Thank you very much!

---

<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:** [11 August 2022 01:21 UTC](https://discourse.nodered.org/t/convert-string-value-to-something-to-work-with/63434/15 "2022-08-11T01:21:41Z")

</div>

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