Referrencing msg.payload in JSON

I attached a debug node to my change node, and changed the debug node to output the complete msg object.

I then clicked the clipboard button and pasted that in here. So I thought that would be the full message.

However, from your response I am guessing it isn't.

Here is a printscreen of the debug node output in the debug tab, hopefully this helps?

debug

Also, if it helps. I am referencing my msg.payload data in my HTML code in a template node (when passing it to an email node) like so:

    <!DOCTYPE html>
<html>
    <head>
{{#payload}}
    <h1>Room Number - {{{Room}}}</h1>
    </head>
<body>
    <p>Email Address - {{{Email}}}</p>
    <p>Password - {{{Password}}}</p>
    </body>
{{/payload}}
</html>

Thanks.

My phone gave up scrolling before I got to anything else. Strangely your screen dump has far fewer spaces in the Password and Email fields.

But from the screenshot it shows that the payload is an array that contains an object.
But if you hover your cursor over the the 123 in the debug window the tooltips should appear that will allow you to copy the path to it.
see https://nodered.org/docs/user-guide/messages

@ukmoose - I have tried copying the path and referencing this in my template node… However this still does not work.

The path it is giving me is: payload[0].Room and so forth…

I really am unsure on what to try next… I have just tried referencing the msg.payload[0].Room in the template node (nothing else) and it isn’t showing the data being passed through.

I have tried the following methods:

{{Room}}
{{payload[0].Room}}
{{msg.payload[0].Room}}
{{msg.payload.Room}}
{{payload.Room}}

Thanks.

That shows your message has msg.number set - which is why using just {{number}} worked in your Template.

It also shows that msg.payload is an array containing a single element which is the object with the Room/Password/Email properties. And it shows that number doesn’t exist in that object. These are all the sorts of small details that are vital to be able to answer this sort of question - and why I asked you to confirm whether the number you wanted was msg.payload.number back in my original reply.

As it is an element in the array, you can reference those in the Template with {{payload.0.Room}} etc (the mustache template format doesn’t do [0] style array references…)

{
    "messages": [
        {
            "to": "{{number}}",
            "body": "Room Number - {{payload.0.Room}} Email - {{payload.0.Email}} Password - {{payload.0.Password}}",
            "from": "Room Details"
        }
    ]
}

Oh, and are you sure you want to include their password in this message? That isn’t very secure.

Nick

@knolleary @ukmoose

Thanks for your help with this guys. So many things to remember haha! That last code you gave me there worked fine Nick!

I am starting to understand all of this a bit more clearly now though. I have actually been able to sort out other parts of my flow that I have been having issues with, without having to ask for advice!

Thanks for the tutoring with this though, it really is appreciated!

Also, regarding the password being sent in the text. Yes I do need this in the text/email. Basically what I have been asked to create is a Room Service system for Hotels.

The idea is, the customer comes in to check into the hotel. The receptionist gives them their room details via email and/or text message. (I may even try and sort out a serial printer to get a physical copy of this, I have tried this in the past but due to not having the suitable hardware I could not get this working properly). The hotel rooms will have a tablet/device in each room with the Node-RED ui on the screen, with the Insert Data Form on it. I am going to have a separate system with the “back end” part of the system, such as updating room details etc…

All of the room details are used for verification on the Room Service form I have setup, so if you enter incorrect room details when submitting a Room Service request, an error message will pop-up stating that the details have been entered incorrectly.

There is quite a lot to the system I have made so far, and that is why I have learnt so so much with it all! And it still isn’t there yet! Not too long to go I would hope though.

It is all very interesting! :grin:

Hi,
I'd like to reference, in a change node, themsg.topic field.

I've tried: $.topic and $.msg.topic, as I can deduce from your post.
But in both case deploying the flow I have the error: ""Invalid 'to' JSON property".
Which is the right syntax?
I can't find a definitive guide to syntax we can use into the nodes.

Thank you very much

Claudio

Show us the complete text you are entering please.

Ok, excuse me, I've solved.
The right syntax was$.topic.
But I had to choose "expression" and not "json" in the to field.
Thank you.

cld

We would still like to see the complete question in context because a lot of people, including me, learn from this forum.

1 Like

Ok, this is the wrong node:
[{"id":"98b6dbec.1356f8","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":220,"wires":[[]]}]

this is the good one:
[{"id":"eff8325f.f473f","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":100,"wires":[[]]}]

They look the same to me?

Bad:

{
 "_id": $.topic,
 "screen": "off"
}

Good:

{
 "_id": $.topic,
 "screen": "off"
}

@SteveMann the key difference is the type of the field. If you select JSON type, then you cannot do any sort of referencing of message properties - it is entirely static. If you select the Expression type then you can use JSONata expressions that reference message properties.

1 Like

You already know that JSON formatting is my Achilles heel, but after importing the two examples, I see no difference between them. What am I missing?

@SteveMann I agree with you, they are both using an expression and both expressions are identical.

Yup, on inspection the nodes are identical. I'll give @ioclaudio the benefit of the doubt and put that down to copy/paste error given he's acknowledged he needed to use the Expression type rather than JSON.

Yes, my error was to use the wrong type for the field.
I used Json instead of Jsonata.

Wrong:
[{"id":"94f7be69.564c3","type":"change","z":"1802dedb.cf8d01","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[[]]}]

Right:
[{"id":"94f7be69.564c3","type":"change","z":"1802dedb.cf8d01","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[[]]}]

cld

I need to pull a global variable using JSON how do I do that
@zenofmud @knolleary

@Zbrooklyn - seeing how this thread is two years old, it would be a good idea if you started a new thread and explained what you are trying to do and what you have done so far.

With that, I'm going to close this thread.