A little help please with using msg data to fill a function outbound message

I have built a thing to alert me daily if any ink cartridge in our printers <= 20 %, I have further rigged the actionable notifications to tell me what ink is low so I can use this to mark it as ordered.

What I cannot figure out is how to pass this inks name in the attributes field to get thet var set. I could use a switch node instead but that would require a lot more code as well as edits every time a new printer is added. In any case this is so much less code.

Any idea how to get the message payload into the proper location in my function node?
Thanks in advance.

It would be the name you gave the variable test

You could use the following:

var test = msg.payload.which

msg.payload = { "attributes": { } }
msg.payload.attributes[test] = "1"

You could also do it without a function node by using a change node and the following JSONata to set msg.payload:

{
    "attributes": {
        payload.which: "1"
    }
}

That’s what I was trying but could not figure out how to use my test variable I kept getting:
Attributes:
test: ink data
as output
Could not figure out how to format the usage.
I see another reply with some options and will try them after a bit, breakfast first.

Thanks

Once a variable is declared in a formula node for the most part, reusing the term inside that same function will result in the value being substituted when executed.

This worked in my function node to send what I wanted, many thanks!
I am having trouble wrapping my head around the use of {}. Not making sense to me yet but hopefully I’ll find the right reference to get it straight in my brain.

Currently using a change node to strip the first few characters from the message returned from my phone and will see if I can get a proper setup using this method as well.

Many thanks

var test = msg.payload.which

msg.payload = {"data": { } }
msg.payload.data = { "attributes": { } }
msg.payload.data.attributes[test] = "1"
return msg;

Look at the debug, you should see something like

payload
    data
      atrributes

{ } Creates the message object formatting so that subsequent keys are indented.