# Add prefix to string and send it to Thingsboard

**URL:** <https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253>\
**Category:** General\
**Created:** [23 March 2022 16:43 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253 "2022-03-23T16:43:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [23 March 2022 16:43 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253/1 "2022-03-23T16:43:54Z")

</div>

Hi, I´m struggeling with a syntax problem:

I only want to add a prefix to a number und convert it into a JSON in order to send it as "key": value to Thingsboard.

```auto
Message: {"attribute1":"value1"}

```

This flow shows my problem: the JSON inject works, but the second doesn´t.  
What am I missing in the example below?

```auto
[
    {
        "id": "1afcdbc96ee82a20",
        "type": "debug",
        "z": "d6aef09d.37155",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1450,
        "y": 3440,
        "wires": []
    },
    {
        "id": "5a47a129618af689",
        "type": "function",
        "z": "d6aef09d.37155",
        "name": "add prefix inputStatus8_1",
        "func": "msg.payload = \"inputStatus8_1: \" + msg.payload;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1070,
        "y": 3480,
        "wires": [
            [
                "a9b8ea3e197d138b",
                "04479bce7a7fbece"
            ]
        ]
    },
    {
        "id": "bf383bae0f832117",
        "type": "trigger",
        "z": "d6aef09d.37155",
        "name": "{\"inputStatus8_1\":0}",
        "op1": "{\"inputStatus8_1\":0}",
        "op2": "",
        "op1type": "json",
        "op2type": "nul",
        "duration": "250",
        "extend": false,
        "overrideDelay": false,
        "units": "ms",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 1050,
        "y": 3280,
        "wires": [
            [
                "14c03378b1f86cc5"
            ]
        ]
    },
    {
        "id": "9ea57338ffa0c977",
        "type": "inject",
        "z": "d6aef09d.37155",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 850,
        "y": 3280,
        "wires": [
            [
                "bf383bae0f832117"
            ]
        ]
    },
    {
        "id": "14c03378b1f86cc5",
        "type": "debug",
        "z": "d6aef09d.37155",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1310,
        "y": 3220,
        "wires": []
    },
    {
        "id": "04479bce7a7fbece",
        "type": "debug",
        "z": "d6aef09d.37155",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1310,
        "y": 3400,
        "wires": []
    },
    {
        "id": "a9b8ea3e197d138b",
        "type": "json",
        "z": "d6aef09d.37155",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1270,
        "y": 3480,
        "wires": [
            [
                "1afcdbc96ee82a20"
            ]
        ]
    },
    {
        "id": "9058b1d4540c74c4",
        "type": "inject",
        "z": "d6aef09d.37155",
        "name": "var number",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 860,
        "y": 3480,
        "wires": [
            [
                "5a47a129618af689"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 March 2022 16:51 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253/2 "2022-03-23T16:51:25Z")

</div>

If you want a JSON object  
Try

```auto
msg.payload = {inputStatus8_1: msg.payload};
return msg;

```

No need for the JSON node.

But this does not need a function node, you could use a change node for this simple operation  
e.g.

```auto
[{"id":"e64e70f5.5a6f5","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":100,"y":2380,"wires":[["d3fd00bd.b91a18"]]},{"id":"d3fd00bd.b91a18","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.inputStatus8_1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":2400,"wires":[["67259c9d.977edc"]]},{"id":"67259c9d.977edc","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":2360,"wires":[]}]

```

If you wanted a JSON string then try

```auto
msg.payload = '{"inputStatus8_1":' + msg.payload +'}';
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 March 2022 17:06 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253/3 "2022-03-23T17:06:17Z")

</div>

> [@E1cid](#):
>
> If you want a JSON object  
> Try
> 
> ```auto
> msg.payload = {inputStatus8_1: msg.payload};
> return msg;
> 
> ```

To be pedantic, that isn't a JSON object, it is a javascript object. JSON is always a string.

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [23 March 2022 17:41 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253/4 "2022-03-23T17:41:33Z")

</div>

Thank you so 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:** [22 May 2022 17:42 UTC](https://discourse.nodered.org/t/add-prefix-to-string-and-send-it-to-thingsboard/60253/5 "2022-05-22T17:42:11Z")

</div>

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