# Solved. Help with JSONata match output

**URL:** <https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134>\
**Category:** General\
**Created:** [19 June 2022 12:25 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134 "2022-06-19T12:25:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 12:25 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/1 "2022-06-19T12:25:55Z")

</div>

Hi,

Busy trying to learn Node-red. I'm trying to search a message using match and appropriate REGEX. I've got this working however the result is an Object with a group and associated array. How am I able to output the message as a string with the value of the group that matched.

In my example flow below I'm trying to have the output of msg.area = bathroom much as the msg.topic does.

> **Code**
>
> ```auto
> [
> {
> "id": "e9ac906fde06ca7e",
> "type": "inject",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "props": [
> {
> "p": "payload"
> },
> {
> "p": "topic",
> "vt": "str"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "",
> "payloadType": "date",
> "x": 220,
> "y": 2200,
> "wires": [
> [
> "05cfb0bb4a03329b"
> ]
> ]
> },
> {
> "id": "05cfb0bb4a03329b",
> "type": "api-current-state",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "server": "2244d24e336a3d2c",
> "version": 3,
> "outputs": 1,
> "halt_if": "",
> "halt_if_type": "str",
> "halt_if_compare": "is",
> "entity_id": "binary_sensor.bathroom_occupancy",
> "state_type": "str",
> "blockInputOverrides": false,
> "outputProperties": [
> {
> "property": "payload",
> "propertyType": "msg",
> "value": "",
> "valueType": "entityState"
> },
> {
> "property": "data",
> "propertyType": "msg",
> "value": "",
> "valueType": "entity"
> },
> {
> "property": "topic",
> "propertyType": "msg",
> "value": "",
> "valueType": "triggerId"
> },
> {
> "property": "area",
> "propertyType": "msg",
> "value": "{\t \"area\":$match(data.entity_id,/^[\\w_]+\\.(\\w+)_.*$/)\t}",
> "valueType": "jsonata"
> }
> ],
> "for": "0",
> "forType": "num",
> "forUnits": "minutes",
> "override_topic": false,
> "state_location": "payload",
> "override_payload": "msg",
> "entity_location": "data",
> "override_data": "msg",
> "x": 510,
> "y": 2200,
> "wires": [
> [
> "e8455e8d022d6cf0",
> "e2f6c8a3bfbb0232"
> ]
> ]
> },
> {
> "id": "058dfb0e82cefc9e",
> "type": "debug",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "topic",
> "targetType": "msg",
> "statusVal": "",
> "statusType": "auto",
> "x": 1020,
> "y": 2240,
> "wires": []
> },
> {
> "id": "e8455e8d022d6cf0",
> "type": "debug",
> "z": "92f311dc4ccbcf60",
> "name": "Area",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "area",
> "targetType": "msg",
> "statusVal": "",
> "statusType": "auto",
> "x": 850,
> "y": 2200,
> "wires": []
> },
> {
> "id": "e2f6c8a3bfbb0232",
> "type": "change",
> "z": "92f311dc4ccbcf60",
> "name": "Area",
> "rules": [
> {
> "t": "change",
> "p": "topic",
> "pt": "msg",
> "from": "^[\\w_]+\\.(\\w+)_.*$",
> "fromt": "re",
> "to": "$1",
> "tot": "str"
> }
> ],
> "action": "",
> "property": "",
> "from": "",
> "to": "",
> "reg": false,
> "x": 830,
> "y": 2240,
> "wires": [
> [
> "058dfb0e82cefc9e"
> ]
> ]
> },
> {
> "id": "2244d24e336a3d2c",
> "type": "server",
> "name": "Home Assistant",
> "version": 2,
> "addon": false,
> "rejectUnauthorizedCerts": true,
> "ha_boolean": "y|yes|true|on|home|open",
> "connectionDelay": true,
> "cacheJson": true,
> "heartbeat": false,
> "heartbeatInterval": 30
> }
> ]
> 
> ```

Debug output  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0475d6f1e67389883e7aae6b296872b0d4b4969c.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 12:29 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/2 "2022-06-19T12:29:37Z")

</div>

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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)

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 12:34 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/3 "2022-06-19T12:34:12Z")

</div>

Thanks for the reply @zenofmud, I don't think I'm explaining myself properly.

I'd like to change the JSONata expression on the current state node such that it outputs the value of the string that has been matched. Apologies I'm a bit of a novice with all this.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b5daf61e1b4d986dc77dffe16f18b96099d3e21.png)

Current expression results in an object where I want it to be a string...?

```auto
{
   "area":$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/)
}

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 12:38 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/5 "2022-06-19T12:38:16Z")

</div>

> [@sbrighty](#):
>
> Current expression results in an object where I want it to be a string...?

Try running it thru the `json` node

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 12:42 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/6 "2022-06-19T12:42:56Z")

</div>

Which converts it to a string, cool thanks. However the string is the full object where I just want the "groups".

Output  
area: "{"area":{"match":"binary\_sensor.bathroom\_occupancy","index":0,"groups":["bathroom"]}}"

Desired Output:  
area: "bathroom"

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 12:54 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/7 "2022-06-19T12:54:05Z")

</div>

Well if you look at the animation in my first post, you will see how to get the path of anything in the payload. Using that you can build an object with the value using that path

---

<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:** [19 June 2022 12:55 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/8 "2022-06-19T12:55:21Z")

</div>

try

```auto
{
   "area":$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]
}

```

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 12:56 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/9 "2022-06-19T12:56:24Z")

</div>

> [@E1cid](#):
>
> `.groups[0]`

Awesome, thanks

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 14:49 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/10 "2022-06-19T14:49:19Z")

</div>

@E1cid I just realized it's still outputting as an object. Got a clever way to turn it into a string?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 15:02 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/11 "2022-06-19T15:02:52Z")

</div>

run it thru the `json` node

---

<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:** [19 June 2022 15:03 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/12 "2022-06-19T15:03:09Z")

</div>

I am guessing blind here, as you supply no data to see what is actually outputted.

```auto
$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]

```

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 15:13 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/13 "2022-06-19T15:13:25Z")

</div>

Apologies.

Output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9b5a21f734bb0cd9650cb554473581a43c38947.png)

I'm trying to get the output of the msg.area to be the value "bathroom" as a string not an object. Thanks again.

Code:

> **Code**
>
> ```auto
> [
> {
> "id": "e9ac906fde06ca7e",
> "type": "inject",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "props": [
> {
> "p": "payload"
> },
> {
> "p": "topic",
> "vt": "str"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "",
> "payloadType": "date",
> "x": 160,
> "y": 2200,
> "wires": [
> [
> "05cfb0bb4a03329b"
> ]
> ]
> },
> {
> "id": "05cfb0bb4a03329b",
> "type": "api-current-state",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "server": "2244d24e336a3d2c",
> "version": 3,
> "outputs": 1,
> "halt_if": "",
> "halt_if_type": "str",
> "halt_if_compare": "is",
> "entity_id": "binary_sensor.bathroom_occupancy",
> "state_type": "str",
> "blockInputOverrides": false,
> "outputProperties": [
> {
> "property": "payload",
> "propertyType": "msg",
> "value": "",
> "valueType": "entityState"
> },
> {
> "property": "data",
> "propertyType": "msg",
> "value": "",
> "valueType": "entity"
> },
> {
> "property": "topic",
> "propertyType": "msg",
> "value": "",
> "valueType": "triggerId"
> },
> {
> "property": "area",
> "propertyType": "msg",
> "value": "{\t \"area\":$match(data.entity_id,/^[\\w_]+\\.(\\w+)_.*$/).groups[0]\t}",
> "valueType": "jsonata"
> }
> ],
> "for": "0",
> "forType": "num",
> "forUnits": "minutes",
> "override_topic": false,
> "state_location": "payload",
> "override_payload": "msg",
> "entity_location": "data",
> "override_data": "msg",
> "x": 470,
> "y": 2200,
> "wires": [
> [
> "e8455e8d022d6cf0",
> "e2f6c8a3bfbb0232"
> ]
> ]
> },
> {
> "id": "058dfb0e82cefc9e",
> "type": "debug",
> "z": "92f311dc4ccbcf60",
> "name": "",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "topic",
> "targetType": "msg",
> "statusVal": "",
> "statusType": "auto",
> "x": 1020,
> "y": 2240,
> "wires": []
> },
> {
> "id": "e8455e8d022d6cf0",
> "type": "debug",
> "z": "92f311dc4ccbcf60",
> "name": "Area",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "area",
> "targetType": "msg",
> "statusVal": "",
> "statusType": "auto",
> "x": 930,
> "y": 2200,
> "wires": []
> },
> {
> "id": "e2f6c8a3bfbb0232",
> "type": "change",
> "z": "92f311dc4ccbcf60",
> "name": "Area",
> "rules": [
> {
> "t": "change",
> "p": "topic",
> "pt": "msg",
> "from": "^[\\w_]+\\.(\\w+)_.*$",
> "fromt": "re",
> "to": "$1",
> "tot": "str"
> }
> ],
> "action": "",
> "property": "",
> "from": "",
> "to": "",
> "reg": false,
> "x": 830,
> "y": 2240,
> "wires": [
> [
> "058dfb0e82cefc9e"
> ]
> ]
> },
> {
> "id": "2244d24e336a3d2c",
> "type": "server",
> "name": "Home Assistant",
> "version": 2,
> "addon": false,
> "rejectUnauthorizedCerts": true,
> "ha_boolean": "y|yes|true|on|home|open",
> "connectionDelay": true,
> "cacheJson": true,
> "heartbeat": false,
> "heartbeatInterval": 30
> }
> ]
> 
> ```

---

<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:** [19 June 2022 15:16 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/14 "2022-06-19T15:16:35Z")

</div>

So my previous should work,  
set msg area  
to  
J: `$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]`

---

<div class="post-metadata">

**Author:** ![sbrighty](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sbrighty/32/63482_2.png) [@sbrighty](https://discourse.nodered.org/u/sbrighty)\
**Post date:** [19 June 2022 15:21 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/15 "2022-06-19T15:21:19Z")

</div>

> [@E1cid](#):
>
> $match(data.entity\_id,/\[1\]+.(\w+)\_.\*$/).groups[0]

So good, thanks!

* * *

1. \w\_

---

<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:** [3 July 2022 15:21 UTC](https://discourse.nodered.org/t/solved-help-with-jsonata-match-output/64134/16 "2022-07-03T15:21:26Z")

</div>

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