# How to filter the values of msg.payload?

**URL:** <https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387>\
**Category:** General\
**Created:** [19 January 2021 12:48 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387 "2021-01-19T12:48:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![relic](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@relic](https://discourse.nodered.org/u/relic)\
**Post date:** [19 January 2021 12:48 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/1 "2021-01-19T12:48:29Z")

</div>

Hi there,

I'm somehow new to Node-RED and I like it very much as I've managed to do some mini personal projects with it.

Right now, I'm stuck with a payload that needs to get filtered out - it looks very similar to the following example as the real one contains hundreds or even thousands of items:

- This is how the payload looks like in the debug window:  
 ![Screenshot-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c6dceba9735d057970c330e0201d4b18cf2fc3a.png)

- This is the payload copy:

```auto
{"file_name":"value","filename":"value","name":"value","test":"value","name_file":"value"}

```

I wish to filter the results of the payload to get only the ones that are ending in 'name'.  
I've been trying to search for an answer and the only one that was somehow useful was this one:  
[https://discourse.nodered.org/t/function-node-to-filter-2-values-out/13296/13?u=relic](https://discourse.nodered.org/t/function-node-to-filter-2-values-out/13296/13)

More specifically, this line of code (the first one gives me 'undefined' in a change node):

```auto
payload ~> | $ | {}, $keys($)~>$filter(/.*_qc/) |

```

The problem with the above code is that I don't understand it in order to modify it to my liking (I don't know what these signs mean or what they are doing `~>`, `| $ |`, `($)` and the last `|` from the end).

If I edit it from `/.*_qc/` to `/name/` or to `/.*name/`or to `/name.*/`, it doesn't really do much - it filters the results, but now how I want to.

This is the payload:

```auto
{"file_name":"value","filename":"value","name":"value","test":"value","name_file":"value"}

```

This is the filtered payload (with the above code):

```auto
{"name":"value","test":"value","name_file":"value"}

```

This is what I'm trying to achieve:

```auto
{"file_name":"value","filename":"value"}

```

Hopefully I wrote everything in detail in order to get some help, examples or guidance to achieve this! 😄

---

<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:** [19 January 2021 13:10 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/2 "2021-01-19T13:10:38Z")

</div>

> [@relic](#):
>
> This is the payload:
> 
> ```auto
> {"file_name":"value","filename":"value","name":"value","test":"value","name_file":"value"}
> 
> ```

> [@relic](#):
>
> This is what I'm trying to achieve:
> 
> ```auto
> {"file_name":"value","filename":"value"}
> 
> ```

function node...

```auto
msg.payload = {
  filename: msg.payload.filename,
  file_name: msg.payload.file_name
}
return msg;

```

* * *

I have to ask why though? Typically, you just use the values you want in the next node and ignore the rest!

---

<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 January 2021 13:35 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/3 "2021-01-19T13:35:37Z")

</div>

Try this JSONata expression

```auto
$sift(payload, function($v,$k){$contains($k,/\wname$/)}) 

```

---

<div class="post-metadata">

**Author:** ![relic](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@relic](https://discourse.nodered.org/u/relic)\
**Post date:** [19 January 2021 14:00 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/4 "2021-01-19T14:00:37Z")

</div>

It worked flawlessly!

Please, may I know how you managed to do it so quickly? I'm not used to write expressions, and if you could give me some guidance about how to create / apply them, that would be great!

Looking forward to your reply before I mark it as Solution.

---

<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:** [19 January 2021 14:10 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/5 "2021-01-19T14:10:57Z")

</div>

I like that JSONata version. Well done 🙂

But as for most, it takes to learn it to get such problems solved.  
Meanwhile there is pretty many ways to do it with known tools...

```auto
function removeKey(k){
    if(k.startsWith('name') || !k.endsWith('name')){
        delete msg.payload[k] 
    }
}
Object.keys(msg.payload).forEach(k => removeKey(k))
return msg;

```

---

<div class="post-metadata">

**Author:** ![relic](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@relic](https://discourse.nodered.org/u/relic)\
**Post date:** [19 January 2021 14:26 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/6 "2021-01-19T14:26:40Z")

</div>

Your method is great as well! Thank you very much!

---

<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 January 2021 14:30 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/7 "2021-01-19T14:30:40Z")

</div>

Jsonata is not the most easy to learn and has it quirks.  
With this problem we need to check the key, so i just used `$sift()` to iterate over the object. The check is if key contains `name` at end ( `$` ) of string with 1 `\w` (word part) in front of it.

As @hotNipi shows there are many other ways, ie another js version.

```auto
for(let key in msg.payload){
    if (!(/\wname$/).test(key)){delete msg.payload[key]}
}
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:** [2 February 2021 14:30 UTC](https://discourse.nodered.org/t/how-to-filter-the-values-of-msg-payload/39387/8 "2021-02-02T14:30:42Z")

</div>

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