# How to find certain text inside an object or a string?

**URL:** <https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354>\
**Category:** General\
**Created:** [25 June 2022 11:12 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354 "2022-06-25T11:12:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [25 June 2022 11:12 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/1 "2022-06-25T11:12:51Z")

</div>

I am pulling data from a package tracking API.

Specifically, the data I'm interested in is the "Last mile" tracking number.

The output looks like this:

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c4e6e6e8813af9c2141bd54c7008857688d3612.jpeg)

Each object in the array is an event in the package's tracking history.

This information that I want to pull is not always in array object number 2. It can be in object 3, or 5, og 10... now I want to be able to pull that tracking number out, format is to remove the text before it, and re-use it further in my flow.

Because it's not in a fixed position, I guess I somehow have to "search" for it?

TL;DR

#1: How to find the pictured `message` key regardless of the position in the array

and

#2: How to parse it into a string with only the tracking number, for further use in the flow.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [25 June 2022 11:24 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/2 "2022-06-25T11:24:36Z")

</div>

It would be nice if you post the complete array, so we can simulate the data with an inject node and can create an example for a flow. Or at least the checkpoint array. I guess the message is only in one of the array elements? If not what else can be used to identify the correct element?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [25 June 2022 11:26 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/3 "2022-06-25T11:26:12Z")

</div>

> [@network\_potato](#):
>
> find the pictured `message` key regardless of the position in the array

But what if there is many of such?

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [25 June 2022 11:47 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/4 "2022-06-25T11:47:41Z")

</div>

Here is the complete message object:

```auto
{"meta":{"code":200},"data":{"tracking":{"checkpoints":[{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Yanwen Pickup scan","country_iso3":null,"tag":"InTransit","subtag":"InTransit_002","subtag_message":"Acceptance scan","checkpoint_time":"2022-05-30T08:08:40+08:00","coordinates":[],"state":null,"zip":null,"raw_tag":"PU10"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Processing information input","country_iso3":null,"tag":"InfoReceived","subtag":"InfoReceived_001","subtag_message":"Info Received","checkpoint_time":"2022-05-30T20:08:40+08:00","coordinates":[],"state":null,"zip":null,"raw_tag":"SC10"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Last mile=> Norway Post, number LM002842896NO","country_iso3":null,"tag":"InTransit","subtag":"InTransit_001","subtag_message":"In Transit","checkpoint_time":"2022-06-01T05:26:41+08:00","coordinates":[],"state":null,"zip":null,"raw_tag":"SC40"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Port of departure - Departure","country_iso3":null,"tag":"InTransit","subtag":"InTransit_007","subtag_message":"Departure Scan","checkpoint_time":"2022-06-04T12:01:00+08:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LH20"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Port of destination - Arrival","country_iso3":null,"tag":"InTransit","subtag":"InTransit_003","subtag_message":"Arrival scan","checkpoint_time":"2022-06-05T13:38:00+08:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LH30"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package is in transit.","country_iso3":null,"tag":"InTransit","subtag":"InTransit_004","subtag_message":"Arrived at destination country","checkpoint_time":"2022-06-10T01:43:52+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM10"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package is in transit.","country_iso3":null,"tag":"InTransit","subtag":"InTransit_001","subtag_message":"In Transit","checkpoint_time":"2022-06-10T01:48:32+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM12"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package is in transit.","country_iso3":null,"tag":"InTransit","subtag":"InTransit_001","subtag_message":"In Transit","checkpoint_time":"2022-06-10T07:06:36+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM12"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package is in transit.","country_iso3":null,"tag":"InTransit","subtag":"InTransit_001","subtag_message":"In Transit","checkpoint_time":"2022-06-13T06:36:01+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM12"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package is in transit.","country_iso3":null,"tag":"InTransit","subtag":"InTransit_001","subtag_message":"In Transit","checkpoint_time":"2022-06-13T09:20:29+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM12"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":"Norway","country_name":null,"message":"Package has arrived at pickup point.","country_iso3":null,"tag":"AvailableForPickup","subtag":"AvailableForPickup_001","subtag_message":"Available for pickup","checkpoint_time":"2022-06-13T16:47:26+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM30"},{"slug":"yanwen","city":null,"created_at":"2022-06-25T07:57:05+00:00","location":null,"country_name":null,"message":"Package has been delivered.","country_iso3":null,"tag":"Delivered","subtag":"Delivered_001","subtag_message":"Delivered","checkpoint_time":"2022-06-18T09:26:24+01:00","coordinates":[],"state":null,"zip":null,"raw_tag":"LM40"}]}}}

```

`message` exists for every array element, but the text inside can be used to identify it because it only appears once.

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [25 June 2022 11:48 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/5 "2022-06-25T11:48:08Z")

</div>

`message` exists for every array element, but the text inside can be used to identify it because it only appears once.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [25 June 2022 12:02 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/6 "2022-06-25T12:02:28Z")

</div>

Here is if "number" in the message is a selection criteria, if not you can modify the switch node to filter out the correct message (i.e. Last mile or something else):

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

```auto
[
    {
        "id": "b7b40bba4aba9f87",
        "type": "debug",
        "z": "53d8621916dd6725",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1010,
        "y": 120,
        "wires": []
    },
    {
        "id": "4bc4cd136a22217e",
        "type": "change",
        "z": "53d8621916dd6725",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.data.tracking.checkpoints",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 310,
        "y": 120,
        "wires": [
            [
                "ccd9852f66008fc0"
            ]
        ]
    },
    {
        "id": "ccd9852f66008fc0",
        "type": "split",
        "z": "53d8621916dd6725",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 490,
        "y": 120,
        "wires": [
            [
                "8224324b3c4c3a1c"
            ]
        ]
    },
    {
        "id": "2da37bce220e6b63",
        "type": "change",
        "z": "53d8621916dd6725",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.message",
                "tot": "msg"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": ".*number\\s(.*)$",
                "fromt": "re",
                "to": "$1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 810,
        "y": 120,
        "wires": [
            [
                "b7b40bba4aba9f87"
            ]
        ]
    },
    {
        "id": "8224324b3c4c3a1c",
        "type": "switch",
        "z": "53d8621916dd6725",
        "name": "",
        "property": "payload.message",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "number",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 630,
        "y": 120,
        "wires": [
            [
                "2da37bce220e6b63"
            ]
        ]
    }
]

```

---

<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:** [25 June 2022 12:12 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/7 "2022-06-25T12:12:15Z")

</div>

Try this using JSONata

```auto
[{"id":"874b0adc0efdef79","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":{\"meta\":{\"code\":200},\"data\":{\"tracking\":{\"checkpoints\":[{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Yanwen Pickup scan\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_002\",\"subtag_message\":\"Acceptance scan\",\"checkpoint_time\":\"2022-05-30T08:08:40+08:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"PU10\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Processing information input\",\"country_iso3\":null,\"tag\":\"InfoReceived\",\"subtag\":\"InfoReceived_001\",\"subtag_message\":\"Info Received\",\"checkpoint_time\":\"2022-05-30T20:08:40+08:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"SC10\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Last mile=> Norway Post, number LM002842896NO\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_001\",\"subtag_message\":\"In Transit\",\"checkpoint_time\":\"2022-06-01T05:26:41+08:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"SC40\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Port of departure - Departure\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_007\",\"subtag_message\":\"Departure Scan\",\"checkpoint_time\":\"2022-06-04T12:01:00+08:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LH20\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Port of destination - Arrival\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_003\",\"subtag_message\":\"Arrival scan\",\"checkpoint_time\":\"2022-06-05T13:38:00+08:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LH30\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package is in transit.\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_004\",\"subtag_message\":\"Arrived at destination country\",\"checkpoint_time\":\"2022-06-10T01:43:52+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM10\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package is in transit.\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_001\",\"subtag_message\":\"In Transit\",\"checkpoint_time\":\"2022-06-10T01:48:32+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM12\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package is in transit.\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_001\",\"subtag_message\":\"In Transit\",\"checkpoint_time\":\"2022-06-10T07:06:36+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM12\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package is in transit.\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_001\",\"subtag_message\":\"In Transit\",\"checkpoint_time\":\"2022-06-13T06:36:01+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM12\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package is in transit.\",\"country_iso3\":null,\"tag\":\"InTransit\",\"subtag\":\"InTransit_001\",\"subtag_message\":\"In Transit\",\"checkpoint_time\":\"2022-06-13T09:20:29+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM12\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":\"Norway\",\"country_name\":null,\"message\":\"Package has arrived at pickup point.\",\"country_iso3\":null,\"tag\":\"AvailableForPickup\",\"subtag\":\"AvailableForPickup_001\",\"subtag_message\":\"Available for pickup\",\"checkpoint_time\":\"2022-06-13T16:47:26+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM30\"},{\"slug\":\"yanwen\",\"city\":null,\"created_at\":\"2022-06-25T07:57:05+00:00\",\"location\":null,\"country_name\":null,\"message\":\"Package has been delivered.\",\"country_iso3\":null,\"tag\":\"Delivered\",\"subtag\":\"Delivered_001\",\"subtag_message\":\"Delivered\",\"checkpoint_time\":\"2022-06-18T09:26:24+01:00\",\"coordinates\":[],\"state\":null,\"zip\":null,\"raw_tag\":\"LM40\"}]}}}}","payloadType":"json","x":90,"y":60,"wires":[["114c4fe7.8520a8"]]},{"id":"114c4fe7.8520a8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$substringAfter($$.payload.payload.data.tracking.checkpoints[$contains(message,\"Last mile=>\")].message, \"number \")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":60,"wires":[["939cb148.16935"]]},{"id":"939cb148.16935","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":60,"wires":[]}]

```

```auto
$substringAfter(
   $$.payload.payload.data.tracking.checkpoints[$contains(message,"Last mile=>")].message,
   "number "
)

```

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [13 July 2022 11:14 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/8 "2022-07-13T11:14:56Z")

</div>

Thank you!

Had to remove an excess `payload` from the string, but it works perfectly!

---

<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:** [27 July 2022 11:15 UTC](https://discourse.nodered.org/t/how-to-find-certain-text-inside-an-object-or-a-string/64354/9 "2022-07-27T11:15:48Z")

</div>

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