JSON problems - again. (New problem I think)

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.

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?

Thanks.

I shall look at the link in a moment.

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:

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

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

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

These two don't work.

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.

You are very close with:

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 ,:

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:

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

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

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

1 Like

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

Thanks very much.

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

Um.....

Code:

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

return msg;

Debug window:

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

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

Ah, yes.

Sorry.

Forgo the .payload part.