How to create an empty context object

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?

Could you use the change node?

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

Like this:

[
    {
        "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:

[{"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.
:wink:

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

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

No space between them any better?

Sorry.

Just offering ideas.

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

Sorry. Above my skill set.

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

Last minute thought:

This way?

[{"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":[[]]}]

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

image

Not for me

Thanks - that does the job :slight_smile:
image

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

1 Like

No problems.

Glad I could help.

All the best.

That's odd, I get the same result using either
image
or
image

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

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

1 Like

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

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

[{"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":[]}]
1 Like

The clue is certainly given in the name :wink:

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:

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

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

And even I know this and I know NOTHING! :slight_smile:

I modded your suggestion which save me having to use a switch node before the change node to test if it exists :slight_smile:
[orig]
image
[corrected]

Good grief! That Jsonata expression is nearly understandable. :crazy_face:

2 Likes