Help with button { } input for JSON string

I've journeyed off and need to make a JSON string to be sent from a button when pressed.

Playing in the function node, I get that to make a msg it needs to be like this:
(example)

msg = {
    payload: '<font color = '+colour+'><i class="fa fa-bullseye fa-2x"></i></font>',
    who: 'Clr'
};

But I am wanting to send a message which will have a structure something like:

msg.payload = "black";
msg.who = "Clr";

Which I get can be done like:

msg = {

(I just showed the other way/structure above.)

So.... If I want to send that from a button when pressed I select the { } option but am stuck how to put the text there.

I'm having difficulty understanding your requirements but based on your previous post that you deleted you're trying to set icon and colour by payload?

Your original code was close. Here it is edited to (hopefully) work...

var x = "red";
msg = {payload: `<font color = "${x}"><i class="fa fa-bullseye fa-2x"></i></font>`};
return msg;

x could of course be from a msg.payload

I got that worked out.

Bit of "sticking things together" with the right control symbols.

var colour = msg.payload;

//node.warn(colour);

msg = {
    payload: '<font color = '+colour+'><i class="fa fa-bullseye fa-2x"></i></font>',
    who: 'Clr'
};
return msg;

No problems - now.

So, that is sending the colour to the next node.

I am also wanting to send "text" to the node as well. So I need to add an extra thing to the payload.

(forgive me not using the right term.)

So I want to send (example)
msg.payload = "(colour)", msg.who = "(some text)"

(Yeah, not the right way. I am struggling.

So rather than send it to a CHANGE node, (or my favourite: a function node) I can set it up in JSON format at the button node and send it.

But I have never used that before and am stuck with how to format the text.

I didn't know that trick.

As you can see I bashed it out with how I posted above.

Thanks.
I don't know what that isn't showing correctly.
But this is the "trick" I didn't know:

msg = {payload: `<font color = "${x}"><i class="fa fa-bullseye fa-2x"></

The "${x}" part

Search template literals js. Very useful.

1 Like

If you want to send JSON from a ui_button it must be RFC valid.

E.g. no comments, double quotes around every property name...

{
  "who" : "me",
  "payload" : "some text"
}

Ah.

Ok.

So it can be more than 1 line?

I was trying to put that on one line, and failing.

{payload: 'black',who:'Clr'}

The button node would show an error.

I'll try what you said now.

(BRB)

Ah! Stupid me.

Forgot the " around the payload and who.

Thanks.

(If you are still looking)

Ok.

So doing it like that it creates a JSON message.

I remember back to them because they are what comes out of a MQTT node too.

So the message "structure" is msg.payload.(sub names)
And to use them after the MQTT node, I put them through a JSON node that breaks them back to the "normal" msg.(name) format.

Or that's what I believe - and it seems to be wrong.

I am again missing an elephant in how to do this.

The idea (big picture) is that I am going to send message from different parts of the flow/s via MQTT - or local links.

But for the sake of it I will say MQTT.

The message will have a "who" (for who sent the message) and a "payload" which will determine the colour of the bullseye. (indicator)

When "all is done" I press a button on that page/dashboad to "clear" the indicator.
That will send a payload = "grey" and a who of "Clr".
To turn the bullseye grey and put the text of Clr beside the indicator.

All that being good, I will feed the button output into the JSON node too and strip it back to normal message "format"/structure.

It doesn't seem to be wanting to play that game.

As I said, don't worry. I'll sit here and bash away at it.

But the elephant is annoying.