# Use variable within JSON

**URL:** <https://discourse.nodered.org/t/use-variable-within-json/67915>\
**Category:** General\
**Created:** [19 September 2022 09:56 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915 "2022-09-19T09:56:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [19 September 2022 09:56 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915/1 "2022-09-19T09:56:05Z")

</div>

```auto
[
    {
        "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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d78913dfff96632656c58d46137862cf35dc166d.jpeg)

---

<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:** [19 September 2022 10:15 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915/2 "2022-09-19T10:15:20Z")

</div>

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

a simple example

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [19 September 2022 10:33 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915/3 "2022-09-19T10:33:59Z")

</div>

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,

---

<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:** [19 September 2022 10:42 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915/4 "2022-09-19T10:42:47Z")

</div>

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

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

```

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

```

---

<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:** [3 October 2022 10:43 UTC](https://discourse.nodered.org/t/use-variable-within-json/67915/5 "2022-10-03T10:43:22Z")

</div>

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