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

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:

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.

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?

But what if there is many of such?

Here is the complete message object:

{"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.

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

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):

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

Try this using JSONata

[{"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":[]}]
$substringAfter(
   $$.payload.payload.data.tracking.checkpoints[$contains(message,"Last mile=>")].message,
   "number "
)

Thank you!

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

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