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

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.

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.

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

1 Like

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

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

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

{
        "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 :grinning:

Here are examples of each

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

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

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