# Checking for a key with a space inside it?

**URL:** <https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827>\
**Category:** General\
**Created:** [27 April 2021 20:30 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827 "2021-04-27T20:30:47Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 20:30 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/1 "2021-04-27T20:30:48Z")

</div>

Hi everyone,

I'm getting an object with a key that got a space in its name and I'm trying to figure out how to create a switch rule for that. Here you can see an example output what I mean:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36c844712153e9514f24c733429b12e9c93a63cf.png)

And this is what I'm trying to achieve:

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

I wouldn't ask if this will work, tryed it also with ["filament\_switch\_sensor Sensor"] but still not working.

So anyone knows how to solve this?

---

<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:** [27 April 2021 20:49 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/2 "2021-04-27T20:49:49Z")

</div>

I've only dealt with this using function nodes I'm afraid. There are several ways to check whether a key exists using JavaScript.

```auto
// This is the SAFEST way
if ( Object.prototype.hasOwnProperty.call(OBJECT, 'key') ) {
    // Key exists so do something
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 April 2021 20:50 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/3 "2021-04-27T20:50:00Z")

</div>

The space should not be an issue , what property is the switch node set to?

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:06 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/4 "2021-04-27T21:06:02Z")

</div>

has key and string as the others (see my screenshot in the original post)

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:06 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/5 "2021-04-27T21:06:57Z")

</div>

that was my thought as well to get it done with a function node... but my hope is still to get this solved with the default switch node 🙂

mhhh tryed it but this solution seems not to work:

```auto
var OBJECT = msg.payload;

// This is the SAFEST way
if ( Object.prototype.hasOwnProperty.call(OBJECT, 'filament_switch_sensor Sensor') ) {
    return msg;
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 April 2021 21:09 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/6 "2021-04-27T21:09:38Z")

</div>

That's not what i'm after. What is the main property the switch is set to, the one under name.  
Also, can we get a complete debug output, of the input to the switch,.

[edit , is the switch also set to check all options or find the first?]

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:11 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/7 "2021-04-27T21:11:19Z")

</div>

ah it is on msg.payload 🙂 sry for that  
and checking ALL rules

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 April 2021 21:15 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/8 "2021-04-27T21:15:44Z")

</div>

Looking at your image, the key is in msg.results.status, but can not tell as the image hides alot, a complete debug output would help.

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:17 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/9 "2021-04-27T21:17:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 April 2021 21:20 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/10 "2021-04-27T21:20:27Z")

</div>

Does it find fan? if it does can I have a debug I can copy and paste so i can do some testing.

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:21 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/11 "2021-04-27T21:21:24Z")

</div>

yep all others work, just that one with the space not.

```auto
{"jsonrpc":"2.0","result":{"status":{"heater_bed":{"temperature":59.70476091770062,"target":60},"filament_switch_sensor Sensor":{"filament_detected":true},"gcode":{},"fan":{"speed":1,"rpm":null},"display_status":{"progress":0.3},"print_stats":{"print_duration":9321.11139888801,"total_duration":9559.235056339006,"filename":"Trunk_Tall_0.16mm_PLA__7h39m.gcode","state":"printing"},"toolhead":{"homed_axes":"xyz","estimated_print_time":61695.956782333335,"print_time":61698.806354975015,"extruder":"extruder"},"extruder":{"target":210,"temperature":209.99919772313908}},"eventtime":96164.416848014},"id":"3041544688"}

```

oh and thank you for spending time helping me out 🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 April 2021 21:26 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/12 "2021-04-27T21:26:29Z")

</div>

That's odd - I've just tested locally and it's working fine for me:

Here's a simple inject-\>switch-\>debug that uses `has key` on `filament_switch_sensor Sensor` and the message passes through.

```auto
[{"id":"fd2dd455.7d85d8","type":"inject","z":"3422d1.25b52d3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":110,"y":300,"wires":[["dba78653.d53ac8"]]},{"id":"dba78653.d53ac8","type":"switch","z":"3422d1.25b52d3","name":"","property":"payload.result.status","propertyType":"msg","rules":[{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":300,"wires":[["b7d541b3.632ae"]]},{"id":"b7d541b3.632ae","type":"debug","z":"3422d1.25b52d3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]}]

```

What version of Node-RED are you using?

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:29 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/13 "2021-04-27T21:29:54Z")

</div>

1.3.2 (no update available, because I'm running it inside ioBroker)

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [27 April 2021 21:31 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/14 "2021-04-27T21:31:39Z")

</div>

There is a difference between yours and mine:

property of the switch node

```auto
[{"id":"8de88255.c03ed","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"fd2dd455.7d85d8","type":"inject","z":"8de88255.c03ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":590,"y":260,"wires":[["dba78653.d53ac8"]]},{"id":"dba78653.d53ac8","type":"switch","z":"8de88255.c03ed","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":850,"y":260,"wires":[["b7d541b3.632ae"]]},{"id":"b7d541b3.632ae","type":"debug","z":"8de88255.c03ed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":260,"wires":[]}]

```

Ah I guess the switchnode can't look deep enough inside the subobjects right?

EDIT: My filament will runout in just a min or so, I will report back (because then the property changes a bit, right now it is just the message from websocket that is published every 10 sec for status update and not for "status changed").

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 April 2021 21:36 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/15 "2021-04-27T21:36:32Z")

</div>

> [@nicedevil](#):
>
> Ah I guess the switchnode can't look deep enough inside the subobjects right?

Right - it only looks at the object you've configure it to look at.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 April 2021 21:41 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/16 "2021-04-27T21:41:10Z")

</div>

Ok setting to payload.result.status returns fan and filament\_switch\_sensor Sensor. Setting to payload does not return fan or filament\_switch\_sensor Sensor.

```auto
[{"id":"b7bd45fb.2b9a8","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":190,"y":680,"wires":[["9ed57356.2c6a78"]]},{"id":"9ed57356.2c6a78","type":"switch","z":"9b3f9f31.c45298","name":"","property":"payload.result.status","propertyType":"msg","rules":[{"t":"hask","v":"fan","vt":"str"},{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":340,"y":680,"wires":[["8d284cfa.0fd74"],["8d284cfa.0fd74"],[]]},{"id":"8d284cfa.0fd74","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":660,"wires":[]}]

```

If the keys you are after are on different object properties you could nest the switch node with another using the a know property that always exists to link to the other switch node.

---

<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:** [11 May 2021 21:41 UTC](https://discourse.nodered.org/t/checking-for-a-key-with-a-space-inside-it/44827/17 "2021-05-11T21:41:39Z")

</div>

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