# How to create an empty context object

**URL:** <https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355>\
**Category:** General\
**Created:** [26 October 2023 10:01 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355 "2023-10-26T10:01:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:01 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/1 "2023-10-26T10:01:23Z")

</div>

So I'm testing if global.test is of type undefined with switch node and if it is, I then want to create it as an empty object

But I can't come up with a method without using function node

Is there 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:** [26 October 2023 10:02 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/2 "2023-10-26T10:02:12Z")

</div>

Could you use the `change` node?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:02 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/3 "2023-10-26T10:02:49Z")

</div>

And how would I use the change node to do it?

---

<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:** [26 October 2023 10:03 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/4 "2023-10-26T10:03:55Z")

</div>

Like this:

```auto
[
    {
        "id": "84d68cafac8af5c6",
        "type": "change",
        "z": "0918ee609bf69fc7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "your_name",
                "pt": "global",
                "to": "[]",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1420,
        "y": 4460,
        "wires": [
            []
        ]
    }
]

```

Oh, sorry... Ok.

Put a `SWITCH` node before this to test if the `global name` undefined.

Like this:

```auto
[{"id":"84d68cafac8af5c6","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"your_name","pt":"global","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1440,"y":4480,"wires":[[]]},{"id":"58014e2d35c563c7","type":"switch","z":"0918ee609bf69fc7","name":"","property":"your_name","propertyType":"global","rules":[{"t":"istype","v":"undefined","vt":"undefined"}],"checkall":"true","repair":false,"outputs":1,"x":1220,"y":4480,"wires":[["84d68cafac8af5c6"]]}]

```

Yeah, ok, you said you are using the `switch.`  
I am really not parsing things too good still.

But I hope that did help - even a little.  
😉

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:10 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/5 "2023-10-26T10:10:49Z")

</div>

That's close but not quite what I'm after

Your solution gives me an object with an array (albeit empty one) in it

I'm after a completely empty object

Like what you get if you use

global.set('homemadeSub', {}); in a function node

---

<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:** [26 October 2023 10:12 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/6 "2023-10-26T10:12:00Z")

</div>

I put a ` ` between the `[]` only because of habits.

No space between them any better?

Sorry.

Just offering ideas.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:13 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/7 "2023-10-26T10:13:11Z")

</div>

`[]`doesn't work either - still gives an empty array

---

<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:** [26 October 2023 10:13 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/8 "2023-10-26T10:13:51Z")

</div>

Sorry. Above my skill set.

What's the reason behind not wanting to use a `function` node?

Last minute thought:

This way?

```auto
[{"id":"84d68cafac8af5c6","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"your_name","pt":"global","to":"{}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1440,"y":4480,"wires":[[]]}]

```

---

<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:** [26 October 2023 10:18 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/9 "2023-10-26T10:18:06Z")

</div>

Doesn't this change node work?  
Ah, I think that's what @Trying_to_learn just suggested.

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:18 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/10 "2023-10-26T10:18:29Z")

</div>

Not for me

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:19 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/11 "2023-10-26T10:19:47Z")

</div>

Thanks - that does the job 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b9c5644e3be577fa97c078285b12478b0fc60b7.png)

> [@Trying\_to\_learn](#):
>
> What's the reason behind not wanting to use a `function` node?

I always want to do stuff in NR without function nodes if possible - I can't tupe very well 🙂

---

<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:** [26 October 2023 10:20 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/12 "2023-10-26T10:20:20Z")

</div>

No problems.

Glad I could help.

All the best.

---

<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:** [26 October 2023 10:26 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/13 "2023-10-26T10:26:55Z")

</div>

That's odd, I get the same result using either  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f476b37ecfe28619d7dd79a12fc69211211e7ea.png)  
or  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/afb9653d7bd1f75ea7d904f5e4fc1dbe447cf6b2.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:33 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/14 "2023-10-26T10:33:00Z")

</div>

Apologies - your right - I was trying so many things!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a79781635f7c9026ca9d4f98f60a5eb80e19f1c.png)

I probably tried entering that but forgot to press deploy when I tested it 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 10:38 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/15 "2023-10-26T10:38:31Z")

</div>

I get confused when I see JSON in the dropdown as in my mind, JSON is text and not an object so I didn't think to try it out thoroughly

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 October 2023 12:09 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/16 "2023-10-26T12:09:05Z")

</div>

> [@cymplecy](#):
>
> But I can't come up with a method without using function node

Have a look at this change node snippet of a flow:

```auto
[{"id":"ab01324c.4824","type":"inject","z":"1d66267b.4c5caa","name":"","topic":"","payload":"90","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["3280794c.eb6396"]]},{"id":"3280794c.eb6396","type":"change","z":"1d66267b.4c5caa","name":"Convert Wind Direction in Degrees To Cardinal points","rules":[{"t":"set","p":"directions","pt":"flow","to":"$exists($flowContext(\"directions\")) ? $flowContext(\"directions\") : [\"N\", \"NNE\", \"NE\", \"ENE\", \"E\", \"ESE\", \"SE\", \"SSE\", \"S\", \"SSW\", \"SW\", \"WSW\", \"W\", \"WNW\", \"NW\", \"NNW\"] ","tot":"jsonata"},{"t":"set","p":"index","pt":"msg","to":"$string($floor((payload / 22.5) + 0.5) % 16)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"directions[\"&index&\"]\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":100,"wires":[["10a54176.da8daf"]]},{"id":"10a54176.da8daf","type":"debug","z":"1d66267b.4c5caa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":120,"wires":[]}]

```

---

<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:** [26 October 2023 12:45 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/17 "2023-10-26T12:45:38Z")

</div>

> [@cymplecy](#):
>
> in my mind, JSON is text and not an object

The clue is certainly given in the name 😉

JSON is certainly gives an object. And a JavaScript object can, mostly, be considered equivalent. The exceptions being that JSON is serializable (hence the text thing) whereas some JS objects are not. As so often in the weird world of JavaScript, you have two concepts that are nearly but not quite the same. :sigh:

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 12:52 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/18 "2023-10-26T12:52:35Z")

</div>

Your not going to start a "JSON" vs "JS Object" language argument are you? 🙂

JSON is NOT an object - it's a text description of an object

And even I know this and I know NOTHING! 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2023 13:10 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/19 "2023-10-26T13:10:04Z")

</div>

I modded your suggestion which save me having to use a switch node before the change node to test if it exists 🙂  
[orig]  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/2251136160fb24692e244235ceeca8de0b757129.png)  
[corrected]

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a996c61278f249c14013ee84a92cc2b1b7f0482.png)

---

<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:** [26 October 2023 13:14 UTC](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355/20 "2023-10-26T13:14:58Z")

</div>

Good grief! That Jsonata expression is nearly understandable. 🤪

[Next page](https://discourse.nodered.org/t/how-to-create-an-empty-context-object/82355.md?page=2)
