# How to process a JSON message with special chars

**URL:** <https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112>\
**Category:** General\
**Created:** [4 January 2022 21:35 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112 "2022-01-04T21:35:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [4 January 2022 21:35 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/1 "2022-01-04T21:35:00Z")

</div>

I have a little battery-powered 6 button scene controller (Zigbee), hooked up to Sonoff Zigbee hub with Tasmota installed.

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

When I press the first button, and pipe the message through JSON node, I get this:

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

The unique thing about this button is: `0006!00: ""`

When I press other buttons, they give the following:  
Button 2: `0006!01: ""`  
Button 3: `0008!02: "015507000000"`  
Button 4: `0008!02: "005507000000"`  
Button 5: `0300!4C: "0145000700000000000000"`  
Button 6: `0300!4C: "0345000700000000000000"`

I don't care about the meaning of these buttons, I just want to be able to do different things in a function node.

This doesn't seem to work, can anyone help me?

(Also pls suggest a better way, the nested IFs are a bit lame!)

```auto
if (msg.topic == "tasmotas/zigbee-hub/B67D/tele/SENSOR") {
    if (typeof msg.payload.ZbReceived != undefined) {
        if (typeof msg.payload.ZbReceived.SceneController1 != undefined) {
            if (typeof msg.payload.ZbReceived.SceneController1["0006!00"] != undefined) {
                msg.payload = 0
            }
            if (typeof msg.payload.ZbReceived.SceneController1["0006!01"] != undefined) {
                msg.payload = 1
            }
            if (typeof msg.payload.ZbReceived.SceneController1["0006!03"] != undefined) {
                if (msg.payload.ZbReceived.SceneController1["0008!02"] == "015507000000") {
                    msg.payload = 2
                }
                if (msg.payload.ZbReceived.SceneController1["0008!02"] == "005507000000") {
                    msg.payload = 3
                }
            }
            if (typeof msg.payload.ZbReceived.SceneController1["0300!4C"] != undefined) {
                if (msg.payload.ZbReceived.SceneController1["0300!4C"] == "0145000700000000000000") {
                    msg.payload = 4
                }
                if (msg.payload.ZbReceived.SceneController1["0300!4C"] == "0345000700000000000000") {
                    msg.payload = 5
                }
            }
            msg.topic = "home/light/gf_pergola/scene"
            return msg
        }
    }
}

```

---

<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:** [4 January 2022 21:37 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/2 "2022-01-04T21:37:40Z")

</div>

You can access the value of these properties using [square bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#bracket_notation).

Also, there is a built-in helper...

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [4 January 2022 21:43 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/3 "2022-01-04T21:43:28Z")

</div>

Hi Steve - thanks. I started writing this forum post before I remembered about square bracket notation, then continued as if the problem was something else. Apologies - I should rename the topic as it's misleading now...

I've updated my first post with the function code I'm using, but it's not feeding anything back. Any idea why that might be?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 January 2022 21:49 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/4 "2022-01-04T21:49:18Z")

</div>

Do you actually need to test for the `[0xxxx]`?  
I have zigbee switches with a sonoff bridge (ie similar output) and test the values the devices send.

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [4 January 2022 21:53 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/5 "2022-01-04T21:53:25Z")

</div>

I think so, the value is empty for certain button presses, i.e. msg.payload.xyz.0006!00 = ""

The object key seems to determine which button is being pressed, not the value.  
Not sure why, I think this Aqara button pad is designed for dimming and other things... I just want to treat it like 6 separate buttons.

---

<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:** [4 January 2022 21:54 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/6 "2022-01-04T21:54:03Z")

</div>

Remove all the excessive testing and stop using typeof X != undefined.

`if (x != null) {` is enough.

Also, if the topic matches, then surely that is enough of a test to determine its a message of interest.

Also, use `node.warn("some message")` in your code to help you understand what parts are being hit

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [4 January 2022 21:55 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/7 "2022-01-04T21:55:18Z")

</div>

> [@Steve-Mcl](#):
>
> `if (x != null) {` is enough.

Thanks. (And for your updated bit about using node.warn) - very useful.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 January 2022 21:56 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/8 "2022-01-04T21:56:27Z")

</div>

> I just want to treat it like 6 separate buttons.

Each value is a "separate" button.

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [4 January 2022 21:57 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/9 "2022-01-04T21:57:13Z")

</div>

> [@bakman2](#):
>
> Each value is a "separate" button

There is no value for some buttons! That's the problem... the key value pair is like this:  
`{"0006!00": ""}`, hence needing to test for the key, not the value

---

<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:** [4 January 2022 23:34 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/10 "2022-01-04T23:34:35Z")

</div>

You could use a lookup object.  
e.g.

```auto
if(msg.payload.ZbReceived != null){
    const lookup = {
        "0006!00": 0,
        "0006!01": 1,
        "0008!02015507000000" : 2,
        "0008!02005507000000": 3,
        "0300!4C0145000700000000000000": 4,
        "0300!4C0345000700000000000000": 5
    }

    let search = Object.entries(msg.payload.ZbReceived.sceneController1);
    search = search.find(e => e[0].includes("!")).join("");
    msg.payload = lookup[search];
}
if(msg.payload === undefined){
    msg = null;
    node.warn("No match!");
}
return msg;

```

---

<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:** [5 March 2022 23:35 UTC](https://discourse.nodered.org/t/how-to-process-a-json-message-with-special-chars/56112/11 "2022-03-05T23:35:13Z")

</div>

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