# JSON structure - checking

**URL:** <https://discourse.nodered.org/t/json-structure-checking/91085>\
**Category:** General\
**Created:** [18 September 2024 21:08 UTC](https://discourse.nodered.org/t/json-structure-checking/91085 "2024-09-18T21:08:33Z")\
**Posts on this page:** 6\
**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:** [18 September 2024 21:08 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/1 "2024-09-18T21:08:33Z")

</div>

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

```auto
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)

```auto
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!

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/afe30b70981353547683c8eced61ad7cae15a8ae.png)

as opposed to

 ![Screenshot from 2024-09-19 07-14-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b0a6a5e4456c27a0dfd088d1e2188c7d92918e8.png)

and

 ![Screenshot from 2024-09-19 07-18-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e4cfab60f0221b29a3b2ac8ec0cc8449d34ffe9f.png)

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

---

<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:** [18 September 2024 21:47 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/2 "2024-09-18T21:47:53Z")

</div>

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors). 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 !

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [18 September 2024 22:30 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/3 "2024-09-18T22:30:34Z")

</div>

> [@Steve-Mcl](#):
>
> Quotes are NEEDED when the property name contains spaces or other odd characters otherwise they are optional.

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)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d5a7f58b997e1b92a5c4875c2cc4d3fd69077d5.png)

> [@Steve-Mcl](#):
>
> Let's put it this way, quotes will always work

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 September 2024 08:29 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/4 "2024-09-19T08:29:51Z")

</div>

> [@jbudd](#):
>
> In an inject node, a change node and a template node, quotes are required around all object keys.

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.

---

<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:** [19 September 2024 09:20 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/5 "2024-09-19T09:20:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 October 2024 09:20 UTC](https://discourse.nodered.org/t/json-structure-checking/91085/6 "2024-10-03T09:20:40Z")

</div>

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