# How to set a field in JSON payload with value from context or environment?

**URL:** <https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843>\
**Category:** General\
**Created:** [31 October 2024 22:41 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843 "2024-10-31T22:41:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![37Rb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/37rb/32/101128_2.png) [@37Rb](https://discourse.nodered.org/u/37Rb)\
**Post date:** [31 October 2024 22:41 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/1 "2024-10-31T22:41:47Z")

</div>

I have a bunch of Change nodes that each set a JSON payload, representing requests to send to a device. One of the fields in each request is a password/PIN that the user needs to set. What is the best way for a user to input their PIN and get it into a field in these JSON payloads?

It seems like having them set their PIN as a global environment variable by going to Settings \> Environment makes sense. But I can't figure out how to get that environment variable into the JSON payload in the Change nodes. I see that I can set the payload entirely with an environment variable but I can't figure out how set it as just a field in the JSON payload.

---

<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:** [31 October 2024 23:27 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/2 "2024-10-31T23:27:02Z")

</div>

If you set a global environment var named password of type credential, then you would just in a change node  
set `msg.` `payload.property_name`  
to `$` `password`  
e.g.

```auto
[{"id":"412822c1a0915510","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"password\":\"\"}","payloadType":"json","x":860,"y":7840,"wires":[["0bcafb87d9cf2ec4"]]},{"id":"0bcafb87d9cf2ec4","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload.password","pt":"msg","to":"password","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":7840,"wires":[["c5875fdf482b668a"]]},{"id":"c5875fdf482b668a","type":"debug","z":"d1395164b4eec73e","name":"debug 2576","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1350,"y":7840,"wires":[]}]

```

Where payload.property\_name could be any property path.

p.s. When you say JSON, do you mean a JSON string or a javascript object? As you can only set the properties(fields as i think you have called them) of an object with a change node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 October 2024 23:40 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/3 "2024-10-31T23:40:38Z")

</div>

If msg.payload is already a JSON object then you can use the change node to set an element of it

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71137c74dc656b63bba46e043a6ea1e0186b31ad.png)

If it isn't already JSON, you can make it so with two entries in the change node

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a0eff0ebc4e4d85b9b409886a5987061a1f51ce.png)

> [@37Rb](#):
>
> It seems like having them set their PIN as a global environment variable by going to Settings \> Environment makes sense.

I assume that each user has their own installation of Node-red?  
Because global environment variables are of course global and cannot be changed without a deploy.

I have a global environment variable set the way you propose.  
Here is a snippet of my flows.json where it is defined

```auto
{
        "id": "a4687c609ba79692",
        "type": "global-config",
        "name": "global-config",
        "env": [
            {
                "name": "foo",
                "value": "bar",
                "type": "str"
            }
        ]
    },

```

This might not be appropriate for a PIN.

I think you can get things into flows\_cred.json, which _might_ be encrypted, sorry I can't recall how.

ps I don't know the difference between a JSON string and a javascript object (either?).  
They sound like tomato and tomato to me 😀

---

<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:** [31 October 2024 23:58 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/4 "2024-10-31T23:58:37Z")

</div>

> [@jbudd](#):
>
> ps I don't know the difference between a JSON string and a javascript object (either?).  
> They sound like tomato and tomato to me 😀

Here are examples of each

```auto
[{"id":"fde2b3af12a6912e","type":"debug","z":"d1395164b4eec73e","name":"debug 2576","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":7580,"wires":[]},{"id":"47b271c80efe0eb8","type":"inject","z":"d1395164b4eec73e","name":"JSon (string)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"test\":1}","payloadType":"str","x":830,"y":7540,"wires":[["d23062f8f87d1da0"]]},{"id":"d23062f8f87d1da0","type":"debug","z":"d1395164b4eec73e","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":7540,"wires":[]},{"id":"e7d36681b8d82345","type":"inject","z":"d1395164b4eec73e","name":"javasript object","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"test\":1}","payloadType":"json","x":840,"y":7580,"wires":[["fde2b3af12a6912e"]]}]

```

And some light reading may offer some insight. [JSON Vs. JavaScript: What is the Difference?](https://www.koombea.com/blog/json-vs-javascript/)

---

<div class="post-metadata">

**Author:** ![37Rb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/37rb/32/101128_2.png) [@37Rb](https://discourse.nodered.org/u/37Rb)\
**Post date:** [1 November 2024 00:27 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/5 "2024-11-01T00:27:40Z")

</div>

That's perfect. Yes, they're objects not strings. Thanks!

---

<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:** [15 November 2024 00:28 UTC](https://discourse.nodered.org/t/how-to-set-a-field-in-json-payload-with-value-from-context-or-environment/92843/6 "2024-11-15T00:28:11Z")

</div>

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