Creating a JSON object?

I have two input nodes, one is a payload string (12:30) and the other is a payload number(19)

Im trying to create a JSON object using the first nodes payload as the key and the second node payload as the value. i.e

{"12:30": 19}

I've tried changing the msg.payload to time and temp respectively, I've tried using the join node, have even tried the template node with:

{{time}}:{{temp}}

but nothing seems to give me the output I require.

Would be grateful of some help here.

JSON is a string
Object is an object
JSON object is not a thing.

The issue you have is you want to combine something from 2 node. Messages NEVER arrive at the input at the same time so you need to use something like a join node.

Demo...

[{"id":"8ca96303.426af","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"prop","payload":"12:30","payloadType":"str","x":500,"y":1280,"wires":[["9f0fd482.d4b6e8"]]},{"id":"c0f0a451.674b98","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"value","payload":"19","payloadType":"num","x":500,"y":1320,"wires":[["9f0fd482.d4b6e8"]]},{"id":"9f0fd482.d4b6e8","type":"join","z":"553814a2.1248ec","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":660,"y":1300,"wires":[["76ad9419.0b24ec"]]},{"id":"76ad9419.0b24ec","type":"function","z":"553814a2.1248ec","name":"","func":"var newPayload = {};\nnewPayload[msg.payload.prop] = msg.payload.value;\nmsg.payload = newPayload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":830,"y":1300,"wires":[["697ff6b5.1bd9e8"]]},{"id":"697ff6b5.1bd9e8","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":1300,"wires":[]}]

By input nodes do you mean dashboard text nodes or something else? If they are dashboard fields, if you use a ui_form then you can get both values in one message when Submit is clicked, so you don't need the Join node.

No Colin, Im using home assistant to supply the data. Thank you for your response though

Ive been told this so many times but I thought as the two input node were triggered by the one inject node things would be ok. But this is exactly what's happening.

Thanks for your explanation and demo, that's helped me a lot.

Looking back over your flow, (which is the way I am try to learn this) I didn't know this option

newPayload[msg.payload.prop]=msg.payload.value;

I knew I had to join the payloads, but I was using:

newPayload = msg.payload.prop + ":" + msg.payload.value;

Thanks again.

That is how you work with JS objects dynamically (you use bracket notation)

e.g. 3 ways of setting msg.payload to a value of 123...

msg.payload    = 123;        //Dot notation
msg["payload"] = 123;        //Bracket notation
var str        = "payload";
msg[str]       = 123;       //Bracket notation with dynamic property name

That is just string concatenation


Read this: Demystifying JavaScript Object vs JSON - it is a decent explanation (4 minute read)