# When array and when object key?

**URL:** <https://discourse.nodered.org/t/when-array-and-when-object-key/98011>\
**Category:** Core Development\
**Tags:** array-of-an-object\
**Created:** [7 July 2025 17:50 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011 "2025-07-07T17:50:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [7 July 2025 17:50 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/1 "2025-07-07T17:50:36Z")

</div>

Hi There,

One of the nice things about assignment is that arrays and objects are automagically created, so if I do the following assignments, everything gets created:

 ![Screenshot 2025-07-07 at 19.41.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/8663c891e8d0f1ee48b23db0580bb95eb164ac64.png)

The following happens:

 ![Screenshot 2025-07-07 at 19.42.28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fb451c59c9937751add5e73c1e4aabf495b58a1f.png)

My question is why is the last twelve interpreted as a keyname for an object and not as an array of twelve elements, as the keys before it are?

So `key2.5.12` creates a key `key2` that becomes an array of 5 with the fifth element being an object with a key '12' with a value of 'dd'. I would have expected `key2` to be an array with the five elements with the fifth value being an array of 12 elements with the twelfth element being 'dd'.

From the examples, it seems that the rule is that the last value is _always_ a key-name for an object.

My question is therefore: is this assumption correct _and_ is it a desired feature or a bug?

If it's a bug, it probably won't be fixed because of backward compatibility ...

```flows
[{"id":"518f89ca72f43970","type":"inject","z":"0dfc0365d20e2613","name":"","props":[{"p":"key1.123","v":"dd","vt":"str"},{"p":"key2.5.12","v":"dd","vt":"str"},{"p":"key3.5.3.12","v":"dd","vt":"str"},{"p":"key4.5.3.1.12","v":"dd","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":876,"y":1628,"wires":[["7b6f6a4bc9e6e2ac"]]},{"id":"7b6f6a4bc9e6e2ac","type":"debug","z":"0dfc0365d20e2613","name":"debug 445","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1177,"y":1495,"wires":[]}]

```

Cheers!

---

<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:** [7 July 2025 20:11 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/2 "2025-07-07T20:11:37Z")

</div>

Feels like a bug to me.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 July 2025 21:34 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/3 "2025-07-07T21:34:32Z")

</div>

Well I am confused because I cannot see how an array comes into being. I would have thought that all of these should be Objects. I am sure that someone will explain why that is not the case 😁

---

<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:** [8 July 2025 00:14 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/4 "2025-07-08T00:14:34Z")

</div>

Yes, normally, you would use brackets to create arrays - have you tried that? Dot notation would normally create objects.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [8 July 2025 05:02 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/5 "2025-07-08T05:02:06Z")

</div>

> [@Buckskin](#):
>
> I cannot see how an array comes into being. I

It has to do with JS dual object addressing:

```auto
> obj = { 1:2, 'a': 3 }
{ '1': 2, a: 3 }
> obj.a
3
> obj['a']
3
> obj.1
obj.1
 ^^

Uncaught SyntaxError: Unexpected number
> obj[1]
2
>

```

An object can be addressed using array syntax or dot-notation. So what happens when I do `obj.key[1]`? Is `key` an array or an object?

The number dot-notation, i.e, `key.1.2.3` seems to be an Node-RED extension of that logic: if objects can be addressed using array notation, why can't arrays be addressed using object dot-notation? `key` here might be an array then.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [8 July 2025 05:06 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/6 "2025-07-08T05:06:08Z")

</div>

> [@TotallyInformation](#):
>
> would use brackets to create arrays

Same behaviour, makes no difference

 ![Screenshot 2025-07-08 at 07.04.57](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdd71d0232ef45745e830176d023801b98cbc684.png)

Gives:

 ![Screenshot 2025-07-08 at 07.05.09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b38d5b57e9ca552dd0b68c79dc7099ff083ea72.png)

brackets and dots are interchangeable in NR - which is actually a good thing (unlike in NodeJS) since it provides consistency but leads to this confusion!

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [8 July 2025 05:11 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/7 "2025-07-08T05:11:05Z")

</div>

> [@TotallyInformation](#):
>
> Feels like a bug to me.

I would say no - I'm sure @dceejay or @knolleary were just waiting for someone to come along and ask why this is so, it's been cunningly planned all along. (I'm thinking of a scene out of Blackadder!)

Having now come to terms with it, I actually find it an elegant solution and saves much time creating nested JSON objects using the Json field - if used correctly.

BTW I did point out that using strings as indices causes objects to be generated: `key[1][2]["3"][4]` becomes

```json
[
    null,
    [
        null,
        null,
        {
            "3": [
                null,
                null,
                null,
                null,
                "snafu"
            ]
        }
    ]
]

```

EDIT: Oh no, the last '4' now is an array if the thing before it is an object .... what is going on....

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [8 July 2025 05:17 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/8 "2025-07-08T05:17:56Z")

</div>

![Screenshot 2025-07-08 at 07.16.38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f2d99314ca587cc1b14f9eafbb06e7b6fa53e3b.png)

dot-four versus bracket-four at the end:

 ![Screenshot 2025-07-08 at 07.17.14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af483e1ebd83e8574dd29ec4fe4d2673ce2f0b21.png)

It's a cunning plan ....

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [8 July 2025 17:05 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/9 "2025-07-08T17:05:47Z")

</div>

Well you learn something new everyday. However, according to MDN [`JavaScript syntax requires properties beginning with a digit to be accessed using bracket notation instead of dot notation`] so technically `msg.key1.123` is not valid.

I will stick to` [numeric]` is an Array, `["numeric"]` is an Object and `."numeric"` is an Object. I probably will not use `.numeric` so I do not confuse myself in 6 months time. 😀

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [8 July 2025 17:35 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/10 "2025-07-08T17:35:57Z")

</div>

> [@Buckskin](#):
>
> technically `msg.key1.123` is not valid.

That's not quite right because what NR is doing is using the characters 1, 2 & 3 these are indistinguishable from the numbers 1, 2 & 3 but they are completely different.

UTF-8b (a little know amendment to the UTF-8 standards) creates completely new range, for the characters representing the numbers. There has been discussion to add extra numbers for the characters, so that there might well be the number A (and it's negative value lower case 'a') but that's still in the works.

The reason for these amendments is that gender-ification of the alphabet has been seen to be unnatural and therefore, room should be given for characters that wish to be numbers, and vice versa.

What will happen to the floating point numbers is anyones guess. Some suggest the point should finally be fixated but no one really knows where.

Binaries, as the name suggestions, are fully compliant with the standard, being able to be both one and zero at the same time.

Hexdecimal are considering renaming themselves to octadecimal.

So NodeRED is doing everything right although it should be using the number open bracket and not the character open bracket.

.... sorry I had to write that 🙂

It's of course all from ChatGPT so I can't be ensure about the amendment, it might also be UTF-8c.

---

<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:** [9 July 2025 11:26 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/11 "2025-07-09T11:26:55Z")

</div>

> [@Buckskin](#):
>
> so technically `msg.key1.123` is not valid.

I agree, this is something inside Node-RED, not standard. Node-RED has a function that allows more flexible access for things like gets and sets and I'm guessing that is what this feature uses.

However, I still think the original reported results constitute a bug in Node-RED because the results are inconsistent.

---

<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:** [7 September 2025 11:27 UTC](https://discourse.nodered.org/t/when-array-and-when-object-key/98011/12 "2025-09-07T11:27:35Z")

</div>

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