JSON structure - checking

Sorry folks. I need to clarify this confusion at my end.

msg.payload = {
        "Who": device_name,
        "Modem": A,
        "UpLink": B
};

Given all other things are set.

This works. Or seems to.

and yet in other nodes I see (example)

msg.payload = {
    Who: "TimePi",
    Device: "Modem",
    Status: "Offline"
}

I'm confused with the fact that in the first one the there are " around the "Who", "Modem" and '"Uplink'` and yet on the second there isn't.

I get that NR is smart enough to allow us (the programmers) to make mistakes and it reads around that and does the right thing.

Which is kind of a blessing.

But other times - (yeah, sod's law, and I can't find an example) it doesn't like/accept me doing that.

Could someone help me and show me the actual rule regarding that.
Then I can (maybe) find this other example and post it to get more help.

Thanks.

UPDATE!

Found an example!

if (msg.payload == true) {
   //
   s = "Online";
} else
   if (msg.payload == false) {
      //
      s = "Offline";
   }

msg1.payload = {
   device: "UpLink",
   Status: s
}

Adding to the confusion is the colours show in the editor.

Screenshot from 2024-09-19 07-12-45

as opposed to

Screenshot from 2024-09-19 07-14-43

and

Screenshot from 2024-09-19 07-18-04

(Does that help or only further confuse the situation?) :wink:

Quotes are NEEDED when the property name contains spaces or other odd characters otherwise they are optional.

Let's put it this way, quotes will always work.

This goes hand in hand with square bracket notation. Square bracket notation will always work for accessing an object property whereas dot notation is only possibly with "good property names"

Ps

This is a JavaScript thing (not a node-red thing, also not JSON). to be clear JSON is ALWAYS a string. JSON stands for JavaScript object notation. I.e a string representation of a JavaScript object. What you are doing here, in a function, in all your examples is creating JavaScript objects. They only "look like" JSON because... Well.. JSON is a string representation of a JavaScript object !

2 Likes

In an inject node, a change node and a template node, quotes are required around all object keys.
The function node is the exception to this rule, where they are optional except if the key contains spaces (etc)

Yes. Always use quotes, no matter what other people do!

1 Like

That is because there you are entering a JSON string. In a function node you are using javascript to define a js object, which is not JSON. This confusion between JSON and javascript objects is one that continually causes comes up. Even in the title of this thread 'JSON' is used, but then the question raised is not about JSON but is with javascript object definition.

1 Like

Thanks @Colin @Steve-Mcl and @jbudd

I have (I hope) become slightly wiser.

Whether I will remember it is a whole other thing.

(Sod's law stepped in) and I tried to force the confusion and - of course - it worked.

So I shall dig out the granite, chisel and carve this new stuff into memory.
Shame I can't mark all as the solution.

Sorry, to the others I didn't mark.

1 Like

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