# Creating a JSON object?

**URL:** <https://discourse.nodered.org/t/creating-a-json-object/38426>\
**Category:** General\
**Created:** [2 January 2021 21:59 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426 "2021-01-02T21:59:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevor](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@trevor](https://discourse.nodered.org/u/trevor)\
**Post date:** [2 January 2021 21:59 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/1 "2021-01-02T21:59:50Z")

</div>

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

```auto
{"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:

```auto
{{time}}:{{temp}}

```

but nothing seems to give me the output I require.

Would be grateful of some help here.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 January 2021 22:14 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/2 "2021-01-02T22:14:12Z")

</div>

> [@trevor](#):
>
> JSON object

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...

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 January 2021 22:16 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/3 "2021-01-02T22:16:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![trevor](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@trevor](https://discourse.nodered.org/u/trevor)\
**Post date:** [2 January 2021 22:18 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/4 "2021-01-02T22:18:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trevor](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@trevor](https://discourse.nodered.org/u/trevor)\
**Post date:** [2 January 2021 22:21 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/5 "2021-01-02T22:21:56Z")

</div>

> [@Steve-Mcl](#):
>
> The issue you have is you want to combine something from 2 node

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.

---

<div class="post-metadata">

**Author:** ![trevor](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@trevor](https://discourse.nodered.org/u/trevor)\
**Post date:** [3 January 2021 05:52 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/6 "2021-01-03T05:52:27Z")

</div>

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

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

```

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

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

```

Thanks again.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 January 2021 09:13 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/7 "2021-01-03T09:13:32Z")

</div>

> [@trevor](#):
>
> Looking back over your flow, (which is the way I am try to learn this) I didn't know this option
> 
> ```auto
> newPayload[msg.payload.prop]=msg.payload.value;
> 
> ```

That is how you work with JS objects dynamically (you use [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#Bracket_notation))

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

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

```

  

> [@trevor](#):
>
> I knew I had to join the payloads, but I was using:
> 
> ```auto
> newPayload = msg.payload.prop + ":" + msg.payload.value;
> 
> ```

That is just string concatenation

* * *

Read this: [Demystifying JavaScript Object vs JSON](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) - it is a decent explanation (4 minute read)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 January 2021 09:13 UTC](https://discourse.nodered.org/t/creating-a-json-object/38426/8 "2021-01-17T09:13:33Z")

</div>

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