Object element naming based on another

Hi,
Sorry for the basic question, new to node-red and all that.
I'm trying to add an element in a JSON object with a function node and the name is conditional to CSV data input ID column.

The setting is that I have some RF tags transmitting ID and temperature and I'd like to assign name depending on who is wearing the tag. Sorry again for this probably simple problem.

Input to function node:
{"ID":"TAG+1 010","temp":27.1}

Function node:

var name = null;
if (msg.ID == "TAG+1 010"){
name = "Alfred";
} else if (msg.ID == "TAG+1 073"){
name = "Cathrine";
}

msg.payload["Person"] = name

return msg;

From that I just get:

{"ID":"TAG+1 010","temp":27.1,"Person":null}

Thanks for any help with this problem!

Hi @FredMA - welcome to the forum.

It looks like the data you are passing into the Function node is inside the payload property. So where you have msg.ID in your two if statements, I think you want msg.payload.ID.

In fact it is a javascript object, not a JSON object. JSON is always a string. It may not seem that important, but using the wrong name can cause confusion.

That worked, naturally! Thanks for indulging a beginner!

Noted! Thanks!

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