# Help with how to do this (JSON parsing of msg.payload)

**URL:** <https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475>\
**Category:** General\
**Created:** [22 June 2019 00:32 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475 "2019-06-22T00:32:44Z")\
**Posts on this page:** 16\
**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:** [22 June 2019 00:32 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/1 "2019-06-22T00:32:44Z")

</div>

I am still working on the idea, but this is the "problem" as of now.

I have 'n' machines tasked with pinging two addresses.

All machines:  
Say: Address 1 and address 2.  
If Address 1 is not ping-able, address 2 is Unknown.  
Address status is ONLINE, OFFLINE, UNKNOWN.

I sort of have how each device does this, but I am not 100% sure just yet. It is still "work in progress".

All three devices then send their data back to a device. (One of the three, but I don't think that has any bearing on the problem, so to keep things separated, I'll say another machine.)

This other machine gets the 3 lots of messages from the devices and needs to then pick the "best" one and display it.

So, say:  
Machine 1: ONLINE, OFFLINE  
Machine 2: OFFLINE, UNKNOWN  
Machine 3 ONLINE, UNKNOWN

I want to see Machine 1's messge.  
I think it is something like (either) assigning a priority or _weight_ to the messages.  
As I see it, it is like this: ONLINE - Highest weight. OFFLINE - second. UNKNOWN - third.

Also - yeah, I know.... - I want to _save_ any message in the event any of the machines can't send their message back to the main one.

I've seen this `msg.queue` node ([this one](https://flows.nodered.org/node/node-red-contrib-msg-queue)) but I am not quite getting how it works.  
I am not understand how the `connected` and `disconnected` parts are detected.

So back to what I want to do.  
I just thought that rather than all machines send **two** messages of the state, the messages are combined into one and sent, making it a bit easier (?well....) on the fourth.  
It only needs to parse one message to determine the _weight_ of that one to then compare to the others.

Then there is the case of two or three have the same weight. Who gets priority?  
In that case I guess it can be the _first_ one in the list of received messages.

Thanks in advance.

---

<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:** [22 June 2019 01:14 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/3 "2019-06-22T01:14:15Z")

</div>

Sorry man. I'll delete that post. I was inappropriate.

---

<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:** [22 June 2019 01:14 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/4 "2019-06-22T01:14:56Z")

</div>

Don't worry. I know I am not the easiest to understand.

That is a normal day for me.

Yes, it would be nice if I could explain things better, but then I wouldn't be me.

I'm working on it now, but am now stuck with the && function to detect two variables being set at the same time.

I found this as a base line:  
`if (var1 ==1 ) && (var2==1)`

So I am wanting to use it with this:  
`if (context.get("Modem") == 1) && (context.get("Uplink") == 1)`

But I am seeing an error:  
 ![Screenshot%20from%202019-06-22%2011-13-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5cdf4a0e8f83f40e9e5274c7e9cf76577bd6a60f.png)

I really must be clocking up the **stupid** points.

---

<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:** [22 June 2019 01:23 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/5 "2019-06-22T01:23:22Z")

</div>

> [@Trying\_to\_learn](#):
>
> found this as a base line:  
> `if (var1 ==1 ) && (var2==1)`

Well if you actually wrote that, you would get an error.

Basic syntax is ...  
`if(something)`

Or in your case ...  
`if((something) && (somethingElse))`

Bottom line is the `if` must be contained by parenthesis.

I.e. `if(X) && (Y)` is illegal.

`if((X) && (Y))` is valid.

---

<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:** [22 June 2019 01:24 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/6 "2019-06-22T01:24:09Z")

</div>

Btw, glad you could see the funny side 🙂

---

<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:** [22 June 2019 01:24 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/7 "2019-06-22T01:24:15Z")

</div>

Ok. Thanks.

This is where I found it:

> [@If ( var1 = true and var2 = true ) then / else](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/6):
>
> Repost your flow as the other guys have said so we can import it. Then document in plain english (words) what real world problem you are trying to solve and your thoughts on the logic that you are using to approach it. We may be able to come up with something for you. Here is an example of how i am using Mytimeout in a simple scenario [{"id":"6ea69ec4.cfb7c","type":"ui\_button","z":"a78930fe.ed232","name":"Manual 8hr Turn on of Circulating Pump","group":"2b345d73.f4d042","order":3,"width":0,"…

---

<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:** [22 June 2019 01:25 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/8 "2019-06-22T01:25:32Z")

</div>

It isn't worth getting upset.

I accept I am stupid.

I've been told that all my life.

I am trying to learn (thus the name) new things.

Ya gotta go through the bad stuff to get to the good stuff.

Just I seem to have a knack of find just the bad stuff.

---

<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:** [22 June 2019 01:27 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/9 "2019-06-22T01:27:29Z")

</div>

Ah man, I didn't mean to imply that. Sorry. I don't think you're stupid. Eccentric hell yeah. A bit mad. Yup. But no, not stupid. You try, you give things a shot, and that is half the battle.

Good luck fella.

---

<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:** [22 June 2019 01:30 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/10 "2019-06-22T01:30:31Z")

</div>

You didn't.

I just know I am.

I just seem to have this inability to explain things and it **really** makes it difficult to explai...

Yeah, anyway.

So, I hope you can understand how I feel when I am trying to ask a question but am unable to word it in a way that anyone else can understand.

That isn't just here. It is every where, every day with every one.

---

<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:** [22 June 2019 01:34 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/11 "2019-06-22T01:34:47Z")

</div>

This is where I am at now with working on this _problem_:

Message 1:  
`{"payload":"Modem Online 11:31:54","device":"Modem","who":"TelePi","time":"11:31:54","_msgid":"c9c140f7.dd8fa","condition":"Online"}`  
Message 2:  
`{"payload":"UpLink Online 11:31:54","device":"UpLink","who":"TelePi","time":"11:31:54","_msgid":"c9c140f7.dd8fa","condition":"Online"}`

Flow code:

```auto
if (msg.device == "Modem")
{
    //
    // Modem detected.
    //
    node.warn("Modem message arrived");
    context.set("Modem",1)
    context.set("Modem_status",msg.condition);
} else
if (msg.device == "UpLink")
{
    //
    // UpLink detected.
    //
    node.warn("UpLink message arrived");
    context.set("UpLink",1)
    context.set("UpLink_status",msg.condition);
}
// if (var1 ==1 ) && (var2==1)

if ((context.get("Modem") == 1) && (context.get("Uplink") == 1))
{
    //
    // both messages received.
    //
    node.status({fill:"yellow",shape:"dot",text:"message"});
    context.set("Modem",0);
    context.set("UpLink",0);
    msg = {device1: context.get("Modem_status"), device2: context.get("UpLink_status")};
    return msg;
}

```

I am seeing both contexts being set, but I am not getting to the `// both messages received.` part.

Confused.

Yeah, ok.

UpLink vs Uplink.

Moving on.

---

<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:** [22 June 2019 01:58 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/12 "2019-06-22T01:58:22Z")

</div>

Also found the other mistakes.

But still can't get that last `if` working.

So I split it into 2 lines.

```auto
if (context.get("Modem") == 1)
{
    //
    if (context.get("UpLink") == 1)
    {

```

---

<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:** [22 June 2019 02:08 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/13 "2019-06-22T02:08:22Z")

</div>

It looks good TBH.

I spot no obvious error.

You have added node.warns() to help debug this so I assume you have witnessed the "Uplink" and "Modem message arrived" messages in the debug side-bar & therefore the next part _should_ work.

All I can suggest it you put debug nodes before and after this function node to ensure its called with the parameters you expect .

NOTE (an observation)...  
I rarely create a new `msg` I often just set properties of the existing `msg` as there is often other info in the `msg` that helps me determine cause of unknown issues or at least give me a clue since it will contain the original values.

For example, instead of

```auto
msg = {device1: context.get("Modem_status"), device2: context.get("UpLink_status")};
return msg;

```

I would do...

```auto
msg.device1 = context.get("Modem_status")
msg.device2 = context.get("UpLink_status");
return msg;

```

...so as not to lose the original contents of `msg` that may prove useful.

Doing this instead means if I attach a debug AFTER the function node, it still has the original `payload` and other properties that help me determine where it came from etc.  
Additionally, I often do temp calcs or store other 'info' vars in the `msg` object (instead of creating local `var`s or local `let`s, as it permits me to see them in the debug output if i made any obvious errors.

e.g...

```auto
if (msg.device == "Modem") {
    //
    // Modem detected.
    //
    msg.info = "Modem message arrived";
    node.warn(msg.info);
    msg.modem = 1;
    context.set("Modem",msg.modem)
    context.set("Modem_status",msg.condition);
} else if (msg.device == "UpLink") {
    //
    // UpLink detected.
    //
    msg.info = "UpLink message arrived";
    node.warn(msg.info);
    msg.uplink = 1;
    context.set("UpLink",msg.uplink)
    context.set("UpLink_status",msg.condition);
}

if ((context.get("Modem") == 1) && (context.get("Uplink") == 1))
{
    //
    // both messages received.
    //
    msg.info2 = "both messages received";
    node.status({fill:"yellow",shape:"dot",text:"message"});
    context.set("Modem",0);
    context.set("UpLink",0);
    msg.device1 = context.get("Modem_status"); 
    msg.device2 = context.get("UpLink_status");
    return msg;
}
//lastly, if we reach here, then one of the context vars are not set! output a Warn()...
node.warn(`msg.device : '${msg.device}' may not match either 'Modem' or 'Uplink'. Select this node & check context values in the side-bar to ensure context.Modem and context.Uplink have a value`); 

```

...by not replacing the original `msg` and adding aditional info to it, the debug side-bar can provide even better diagnostics.

Hope that helps somewhat (even if I cant see the issue in your code).

* * *
Ps, one bit of additional advice... better titles. 

This topic would have possibly been better having a tile like "error in function node - expected identify and instead saw &&"

It permits potential helpers to know if it's something they are capable of assisting with.

E.g. if I see a post titled "Alexa isn't responding", I just bypass it as I don't have any Alexa experience (and hate Amazons dodgy sales practices with a passion).

However if I see a title "get value from JSON" I know I can help.

---

<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:** [22 June 2019 02:12 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/14 "2019-06-22T02:12:52Z")

</div>

Yeah, we kinda got off track a bit.

The other thing that was (note: **was** ) killing me was the syntax.

`msg = {"Who":msg.who,"device1": context.get("Modem_status"), "device2": context.get("UpLink_status")};`

The "device1" and "device2" need the quotes around them.  
I for got that and so the whole thing just died.

I may adopt that idea of how to create the messages that way as it is a bit easier to see/read/understand what is going on.

Thanks.

---

<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:** [22 June 2019 02:52 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/15 "2019-06-22T02:52:51Z")

</div>

I'm still stuck. But further along the time line.

This time it is to do with JSON stuff I think, and sending the message to MQTT.

I usually have no problems, but there again: I seem to be good at making my own life difficult.

So:  
I have the message and it is working fine.  
This is the message which I made:  
`{"topic":"TelePi","Who":"TelePi","Modem":"Online","Uplink":"Online","_msgid":"c9c140f7.dd8fa"}`

But what I forgot to declare was that is going into MQTT.

From memory, MQTT only deals with msg.topic and msg.payload.

So, if I want to get other things through, I have to put them as part of msg.payload.  
Like:  
msg.payload.who  
msg.payload.modem  
msg.payload.uplink

for instance.

But editing the `function` node and prefixing the names with payload doesn't seem to work.

I get a nice looking thing being sent to MQTT, but it doesn't get through.

The message becomes this:  
`{"topic":"TelePi","payload.Who":"TelePi","payload.Modem":"Online","payload.Uplink":"Online","_msgid":"c9c140f7.dd8fa"}`

But it isn't getting through MQTT for some reason.

Sussed it out  
I need to make the line:  
`msg.payload = {"part 1":blah, "part 2": blahblah};

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 June 2019 07:09 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/16 "2019-06-22T07:09:05Z")

</div>

But yes, as Steve said, please go back and edit the title of this topic to be more descriptive

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 June 2019 10:49 UTC](https://discourse.nodered.org/t/help-with-how-to-do-this-json-parsing-of-msg-payload/12475/17 "2019-06-22T10:49:22Z")

</div>

> [@Steve-Mcl](#):
>
> Eccentric hell yeah. A bit mad. Yup

Recon that applies to just about all of us here 🤪
