Use variable within JSON

[
    {
        "id": "fd495236339b5cbc",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "4c14b70f4a5557f7",
        "type": "debug",
        "z": "fd495236339b5cbc",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 260,
        "wires": []
    },
    {
        "id": "9702de48f4f9c290",
        "type": "mikrotik",
        "z": "fd495236339b5cbc",
        "device": "81d5cece8f85c408",
        "name": "",
        "action": "9",
        "command": "",
        "command-type": "str",
        "x": 360,
        "y": 260,
        "wires": [
            [
                "4c14b70f4a5557f7",
                "d9e7376d13704871"
            ]
        ]
    },
    {
        "id": "f3f8497682a3e43e",
        "type": "inject",
        "z": "fd495236339b5cbc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[\"/ip/route/print\",\"?comment=NodeRed\"]",
        "payloadType": "json",
        "x": 170,
        "y": 260,
        "wires": [
            [
                "9702de48f4f9c290"
            ]
        ]
    },
    {
        "id": "d9e7376d13704871",
        "type": "change",
        "z": "fd495236339b5cbc",
        "name": "New Raw",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "[\"/ip/route/disable\",\"?.id= msg.payload[0].distance\"]",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 620,
        "y": 300,
        "wires": [
            [
                "4c14b70f4a5557f7"
            ]
        ]
    },
    {
        "id": "81d5cece8f85c408",
        "type": "mikrotik-device",
        "host": "172.16.0.34",
        "port": "8729",
        "username": null,
        "password": null,
        "ssl": "api-ssl-ignore-cert"
    }
]

Here is my code. I want to use one of the values from the Mikrotik step in "New Raw" step. I am not able to use variables within it. I am still learning so apologies if something is not clear.
Here is the output from the Mikrotik step.

Screenshot 2022-09-19 152301

You need to select JSONata J:, as it can build json and insert msg variables etc

a simple example

[{"id":"f3f8497682a3e43e","type":"inject","z":"fd495236339b5cbc","name":"","props":[{"p":"payload[0].distance","v":"200","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":260,"wires":[["d9e7376d13704871"]]},{"id":"d9e7376d13704871","type":"change","z":"fd495236339b5cbc","name":"New Raw","rules":[{"t":"set","p":"payload","pt":"msg","to":"[    \"/ip/route/disable\",    \"?.id=\" & $$.payload[0].distance]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":300,"wires":[["4c14b70f4a5557f7"]]},{"id":"4c14b70f4a5557f7","type":"debug","z":"fd495236339b5cbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":260,"wires":[]}]

You could also use a function node or a template node to do similar.

Perfect. How would I use the message variable ".id" in the next step? I tried using it but only .id is not working rest all works well.

Thanks,

.id Has a dot in front, so would need to be put in quotes.
e.g.

   [
   "/ip/route/disable",
   "?" & $$.payload[0].'.id' & "=" & $$.payload[0].distance
]

[{"id":"d9e7376d13704871","type":"change","z":"fd495236339b5cbc","name":"New Raw","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t   \"/ip/route/disable\",\t   \"?\" & $$.payload[0].'.id' & \"=\" & $$.payload[0].distance\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":300,"wires":[["4c14b70f4a5557f7"]]},{"id":"f3f8497682a3e43e","type":"inject","z":"fd495236339b5cbc","name":"","props":[{"p":"payload[0].distance","v":"200","vt":"num"},{"p":"payload[0][\".id\"]","v":"q","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":260,"wires":[["d9e7376d13704871"]]},{"id":"4c14b70f4a5557f7","type":"debug","z":"fd495236339b5cbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":260,"wires":[]}]
1 Like

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