Add prefix to string and send it to Thingsboard

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.

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?

[
    {
        "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"
            ]
        ]
    }
]

If you want a JSON object
Try

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.

[{"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

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.

Thank you so much!

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