# Subflow - extract value from json

**URL:** <https://discourse.nodered.org/t/subflow-extract-value-from-json/22932>\
**Category:** General\
**Created:** [10 March 2020 14:42 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932 "2020-03-10T14:42:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AungKL](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@AungKL](https://discourse.nodered.org/u/AungKL)\
**Post date:** [10 March 2020 14:42 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932/1 "2020-03-10T14:42:30Z")

</div>

Hello,  
I'm a beginner to node red. I'm trying to create subflow and its environment variables. I have problem with extracting value from json payload. The flow is as follow:

```auto
[{"id":"9b706329.bc293","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"70ccbc5b.6212d4"}]}],"out":[{"x":340,"y":80,"wires":[{"id":"70ccbc5b.6212d4","port":0}]}],"env":[{"name":"msgTopic","type":"str","value":"MBLightTopic"},{"name":"msgValue","type":"env","value":"msg.paylad.MBLight"}],"color":"#DDAA99"},{"id":"70ccbc5b.6212d4","type":"function","z":"9b706329.bc293","name":"MBLight","func":"var msgTopic = env.get(\"msgTopic\");\nvar val_MBLight = msg.payload.MBLight; // <-- How to decleare in Environment Variables\n\nif(msg.topic == msgTopic) { msg.payload = val_MBLight; return msg; }\nelse { return null; } ","outputs":1,"noerr":0,"x":200,"y":80,"wires":[[]]},{"id":"fc8310a7.85ada","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"dd849f09.d85a9","type":"inject","z":"fc8310a7.85ada","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":220,"wires":[["ef283d9c.701cc"]]},{"id":"ef283d9c.701cc","type":"function","z":"fc8310a7.85ada","name":"MBLight","func":"var msgTopic = \"MBLightTopic\";\nvar val_MBLight = msg.payload.MBLight;\nif(msg.topic == msgTopic) { msg.payload = val_MBLight; return msg; }\nelse { return null; } \n\n","outputs":1,"noerr":0,"x":440,"y":220,"wires":[["e97c1575.5e9718"]]},{"id":"e97c1575.5e9718","type":"debug","z":"fc8310a7.85ada","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":220,"wires":[]},{"id":"d1a4c569.e21068","type":"inject","z":"fc8310a7.85ada","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":380,"wires":[["9b5eba1f.eef108"]]},{"id":"6bfdad95.bf0a64","type":"debug","z":"fc8310a7.85ada","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":380,"wires":[]},{"id":"9b5eba1f.eef108","type":"subflow:9b706329.bc293","z":"fc8310a7.85ada","name":"","env":[],"x":440,"y":380,"wires":[["6bfdad95.bf0a64"]]},{"id":"f35567a5.940118","type":"inject","z":"fc8310a7.85ada","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["ef283d9c.701cc"]]},{"id":"af85c38.08a914","type":"inject","z":"fc8310a7.85ada","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["9b5eba1f.eef108"]]}]

```

 ![2020-03-10 22_57_16-Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/0110a13e9c20ee8e5b31fee5e4203c3189203fdd.jpeg)

 ![2020-03-10 22_34_04-Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2cc1971494b0a8701b285ca20d3041a53a709d74.jpeg)

![2020-03-10 22_52_44-Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed3491caf6895f682e50a7dbd823967d4aca2116.jpeg)

How to declare "msg.payload.MBLight" in environment variable of subflow?

Best regards,  
Aung

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [10 March 2020 16:50 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932/2 "2020-03-10T16:50:03Z")

</div>

> [@AungKL](#):
>
> How to declare "msg.payload.MBLight" in environment variable of subflow?

Why would you want to do so ? You already have access to all msg properties inside the subflow. The second line in your function node is already using `msg.payload.MBLight`, right ? We use "environmental variables" (I prefer to use the wording instance variables) when we need to inject values that does not exist as properties of the msg object. These values allow us to customise the many subflows instances for a different behaviour.

---

<div class="post-metadata">

**Author:** ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)\
**Post date:** [10 March 2020 21:23 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932/3 "2020-03-10T21:23:07Z")

</div>

Do you want to specify a particular key name in subflow's environment variables?

That's my guess, so if correct, then:

- use string value for the name of the key,
- use bracket notation to access the key inside the `msg.payload`.

```auto
[{"id":"b87b90dd.a04a3","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"af477576.468718","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"471337c2.b35128"}]}],"out":[{"x":340,"y":80,"wires":[{"id":"471337c2.b35128","port":0}]}],"env":[{"name":"msgTopic","type":"str","value":"MBLightTopic"},{"name":"msgValue","type":"str","value":"msg.payload.MBLight"}],"color":"#DDAA99"},{"id":"471337c2.b35128","type":"function","z":"af477576.468718","name":"MBLight","func":"var msgTopic = env.get(\"msgTopic\");\nvar val_Light = msg.payload[env.get(\"msgValue\")]; // <-- How to decleare in Environment Variables\n\nif(msg.topic == msgTopic) { msg.payload = val_Light; return msg; }\nelse { return null; } ","outputs":1,"noerr":0,"x":200,"y":80,"wires":[[]]},{"id":"95fd80d5.94b35","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":220,"wires":[["99115b7.f606ca8"]]},{"id":"99115b7.f606ca8","type":"function","z":"b87b90dd.a04a3","name":"MBLight","func":"var msgTopic = \"MBLightTopic\";\nvar val_MBLight = msg.payload.MBLight;\nif(msg.topic == msgTopic) { msg.payload = val_MBLight; return msg; }\nelse { return null; } \n\n","outputs":1,"noerr":0,"x":440,"y":220,"wires":[["597bbf8a.d54df"]]},{"id":"597bbf8a.d54df","type":"debug","z":"b87b90dd.a04a3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":220,"wires":[]},{"id":"e89a4d70.f904","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":380,"wires":[["a06d5b92.476078"]]},{"id":"8004c464.4ad7d8","type":"debug","z":"b87b90dd.a04a3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":380,"wires":[]},{"id":"a06d5b92.476078","type":"subflow:af477576.468718","z":"b87b90dd.a04a3","name":"","env":[{"name":"msgValue","value":"MBLight","type":"str"}],"x":440,"y":380,"wires":[["8004c464.4ad7d8"]]},{"id":"ef0b979e.d52548","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["99115b7.f606ca8"]]},{"id":"4908c60d.935248","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["a06d5b92.476078"]]}]

```

---

<div class="post-metadata">

**Author:** ![AungKL](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@AungKL](https://discourse.nodered.org/u/AungKL)\
**Post date:** [11 March 2020 03:33 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932/4 "2020-03-11T03:33:41Z")

</div>

@Andrei, I'd like to create subflow as reusable function with input parameters like c++ function. thanks

@techraf, thank you for help

---

<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:** [25 March 2020 03:33 UTC](https://discourse.nodered.org/t/subflow-extract-value-from-json/22932/5 "2020-03-25T03:33:45Z")

</div>

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