# JSONata $filter

**URL:** <https://discourse.nodered.org/t/jsonata-filter/79494>\
**Category:** General\
**Created:** [29 June 2023 22:15 UTC](https://discourse.nodered.org/t/jsonata-filter/79494 "2023-06-29T22:15:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quesnt](https://avatars.discourse-cdn.com/v4/letter/q/839c29/32.png) [@Quesnt](https://discourse.nodered.org/u/Quesnt)\
**Post date:** [29 June 2023 22:15 UTC](https://discourse.nodered.org/t/jsonata-filter/79494/1 "2023-06-29T22:15:45Z")

</div>

I posted [here](https://discourse.nodered.org/t/change-node-not-sure-how-to-reference-payload-in-expression/79204) about a merge expression I was attempting to get working and received an answer. However, I notice that the match expression includes all records for Obj1 regardless of whether there is a matching key or not. I only want to return records that match and return no records in the output that don't match on the key.

I have read up on the filter function and tried to apply it to this scenario but it is not working. I also tried chatGPT and it also gave a answer that is invalid, according to [JSONata.org](http://JSONata.org) and the test function on the change node, "Expected ")", got ";""

Input:

```auto
{"payload":[
        {
            "Obj1": [
                {
                    "Datetime": "2020-04-03 01:21:07.650",
                    "ID": "00001",
                    "code": "WAXXNNNX"
                },
                {
                    "Datetime": "2020-04-03 01:21:07.650",
                    "ID": "00002",
                    "code": "CAXXNNNX"
                }
            ]
        },
        {
            "Obj2": [
                {
                    "X102": "Y",
                    "X116": "N",
                    "X435": "00001"
                },
                {
                    "X102": "Y",
                    "X116": "Y",
                    "X435": "00002"
                }
            ]
        }
    ]}

```

Expression:

```auto
$$.payload[0].Obj1[$filter(Obj1, 
    $Obj2 := $$.payload[1].Obj2[$.X435 = $.ID];
    $Obj2 != null
).(
    {
        'ID': $.ID,
        'Datetime': $.Datetime,
        'Code': $.code,
        'X102_ZipSuffixFlag': $Obj2.X102,
        'X116_TerminationIDFlag': $Obj2.X116
    }
)]

```

Any help is appreciated, thanks!

---

<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:** [30 June 2023 06:22 UTC](https://discourse.nodered.org/t/jsonata-filter/79494/2 "2023-06-30T06:22:22Z")

</div>

You could just check Obj2 contains a match, then only return the new object if true.  
e.g.

```auto
[$$.payload[0].Obj1@$Obj1.(
   $Obj2 := $$.payload[1].Obj2[$.X435 = $Obj1.ID];
   $exists($Obj2) ? {
       'ID': $Obj1.ID,
       'Datetime': $Obj1.Datetime,
       'Code': $Obj1.code,
       'X102_ZipSuffixFlag': $Obj2.X102,
       'X116_TerminationIDFlag': $Obj2.X116     
   } 
)]

```

[edit]  
To satisfy my curiosity i decided to see if you could use a ternary conditional on a variable binding, and it turns out you can, so the $exists() is not required.  
e.g.

```auto
[$$.payload[0].Obj1@$Obj1.(
   $Obj2 := $$.payload[1].Obj2[$.X435 = $Obj1.ID] ? {
       'ID': $Obj1.ID,
       'Datetime': $Obj1.Datetime,
       'Code': $Obj1.code,
       'X102_ZipSuffixFlag': $Obj2.X102,
       'X116_TerminationIDFlag': $Obj2.X116     
   } 
)]

```

Works also.

---

<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:** [14 July 2023 06:23 UTC](https://discourse.nodered.org/t/jsonata-filter/79494/3 "2023-07-14T06:23:01Z")

</div>

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