# JSON problems - again. (New problem I think)

**URL:** https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066
**Category:** General
**Created:** [9 July 2019 09:08 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066 "2019-07-09T09:08:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 July 2019 09:08 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/1 "2019-07-09T09:08:54Z")

</div>

I've been down the `JSON` track a bit.

When using `MQTT` at the remote end I need to pass the data through a `JSON` node to see the sub sections.

Hoping I have got the idea good, I embarked on this new quest. Partly inspired by my JacaScript curiosity.

I'm wanting to process incoming messages. Usually from `MQTT`.  
So I can _cheat_ and put them through the `JSON` node and get their sub-sections, like:

msg.payload.part1  
msg.payload.part2  
msg.payload.part3

Too easy.  
But then I fell over, flat on my face.

This works fine with stuff sent over `MQTT`, but not for local stuff.  
I don't know how to make messages like they are after coming out of the `JSON` node (and `MQTT`)

I'm sure there is a way, and I probably have been told.  
Just with all the new stuff going in, some stuff got moved (in the brain) and I can't remember.

Other than putting it through `MQTT`, is there a way to get a message in that kind of structure?  
I've tried putting it through the `JSON` node, but it doesn't do what I want.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [9 July 2019 09:22 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/2 "2019-07-09T09:22:06Z")

</div>

> [@Trying\_to\_learn](#):
>
> I don't know how to make messages like they are after coming out of the `JSON` node (and `MQTT` )

Taken from the `JSON` node info panel.

> Converts between a JSON string and its JavaScript object representation, in either direction.

So I'm guessing you mean a javascript object?

> **[JavaScript Objects](https://www.w3schools.com/js/js_objects.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 July 2019 09:24 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/3 "2019-07-09T09:24:29Z")

</div>

Thanks.

I shall look at the link in a moment.

> [@ukmoose](#):
>
> Converts between a JSON string and its JavaScript object representation, in either direction.

Yeah, and I put my "message" in to it and it didn't do what was needed. So you may be right that I want a javascript object.

Sorry, all these new names..... "Stack overflow".

yes, "objects" are what were needed.

But I don't get how to create messages. That link shows how to make them, but not how to send them.

Ok, here's where I'm stuck.

Say I have these variables:

```auto
msg.who = "TimPi",
msg.modem="Online"
msg.uplink="Online"

```

I am wanting to get them as: (forgive the syntax)

```auto
msg.payload.who = "TimePi", msg.payload.modem = "Online", msg.payload.uplink = "Onlinle"

```

These two don't work.

```auto
msg = {payload.who = "TimePi", msg.payload.modem = "Online", msg.payload.uplink = "Online"};
msg = {payload.who: "TimePi", msg.payload.modem: "Online", msg.payload.uplink: "Online"};

```

I can't copy the errors, but I am sure you know them.

Yeah: I'm stupid. I am not getting it. Sorry, that's where I am.  
All I can do is try to learn. And believe me it is just as painful for me as it is for you listening to my repeated questions.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 July 2019 09:45 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/4 "2019-07-09T09:45:46Z")

</div>

You are very close with:

```auto
msg.payload.who = "TimePi", msg.payload.modem = "Online", msg.payload.uplink = "Onlinle"

```

The piece you missed was setting `msg.payload` to be an Object first (and also you need to separate statements with a `;` not a `,`:

```auto
msg.payload = {};
msg.payload.who = "TimePi";
msg.payload.modem = "Online";
msg.payload.uplink = "Online";

```

This example is four separate JavaScript statements. The first one initialises `msg.payload` as an empty object. The next three statements set individual properties of that object.

That can also be written as a single JavaScript statement:

```auto
msg.payload = {
   who: "TimePi",
   modem: "Online",
   uplink: "Online"
};

```

This example sets `msg.payload` as a object that already contains the three values you want.

> [@Trying\_to\_learn](#):
>
> Sorry, all these new names..... "Stack overflow".

No-one has mentioned StackOverflow in this topic... but I assume you have by now discovered this is referring to [https://stackoverflow.com/](https://stackoverflow.com/) which is a very useful resource of programming questions and answers.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 July 2019 09:48 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/5 "2019-07-09T09:48:00Z")

</div>

I'd give you a hug, but I think people will talk.

Thanks very much.

"Stack overflow"....... brain overload.  
Sorry. Local slang.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 July 2019 10:21 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/6 "2019-07-09T10:21:27Z")

</div>

Um.....

Code:

```auto
msg = {};
msg.payload.Who = "TimePi";
msg.payload.device = "Modem";

return msg;

```

Debug window:

```auto
"TypeError: Cannot set property 'Who' of undefined"

```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [9 July 2019 10:22 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/7 "2019-07-09T10:22:25Z")

</div>

Try comparing your pasted code to the code Nick used in his example…

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 July 2019 10:24 UTC](https://discourse.nodered.org/t/json-problems-again-new-problem-i-think/13066/8 "2019-07-09T10:24:58Z")

</div>

Ah, yes.

Sorry.

Forgo the `.payload` part.
