# JSONATA Array Filtering

**URL:** <https://discourse.nodered.org/t/jsonata-array-filtering/92640>\
**Category:** General\
**Created:** [24 October 2024 19:05 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640 "2024-10-24T19:05:50Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [25 October 2024 17:16 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/21 "2024-10-25T17:16:49Z")

</div>

Clearly I made a mistake in referring to it as an array, but I provided precisely what the input data looks like (i.e. now I understand that it's nested objects).

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [25 October 2024 17:20 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/22 "2024-10-25T17:20:37Z")

</div>

I understand, that's what I suspected, that it's simply a limitation of the debug node.

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [25 October 2024 17:34 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/23 "2024-10-25T17:34:11Z")

</div>

Still confused, I did provide the example of how I wanted to filter the data (conditional values) - in fact, you provided the jsonata expression to do that ?

```auto
[{"id":"29b7a996ff9053cc","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"change","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"price_min","pt":"msg","to":"100","tot":"num"},{"t":"set","p":"price_max","pt":"msg","to":"300","tot":"num"},{"t":"set","p":"payload","pt":"msg","to":"$sift($$.payload.data.data, function($v,$k)\t{$v.change > $$.change and $v.price > $$.price_min and $v.price < $$.price_max}\t)\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":7840,"wires":[["4e5340ffce155365"]]},{"id":"4ad42215ec3280f3","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"status\": 200, \"data\": { \"data\": { \"AAPL\": { \"volume\": 18457363, \"price\": 230.42, \"change\": -0.15, \"float\": 15179469955, \"relativeVolume\": 36.30859039199602, \"shortRatio\": 2.32, \"shortFloat\": 0.93377, \"daysGap\": -0.34, \"averageVolume\": 39185859.6, \"rsi\": 52.096 }, \"NVDA\": { \"volume\": 130080525, \"price\": 139.7328, \"change\": 0.12, \"float\": 23541535433, \"relativeVolume\": 40.00929295738587, \"shortRatio\": 0.8, \"shortFloat\": 1.06658, \"daysGap\": 0.9, \"averageVolume\": 254269218.6, \"rsi\": 62.832 } } } }","payloadType":"json","x":190,"y":7840,"wires":[["29b7a996ff9053cc"]]}]

```

This expression generated an output (of nested objects), which I then need to split, and that resulted in loosing the stock symbol (i.e. ADT) so I experimented in converting it to the output format that I want:

```auto
[{"volume":29079654,"price":8.14,"change":17.63,"float":277767777,"relativeVolume":669.3863072501292,"shortRatio":5.15,"shortFloat":8.47932,"daysGap":6.65,"name":"ADT"},{"volume":68074619,"price":6.5,"change":25.48,"float":371136076,"relativeVolume":1183.5067008672897,"shortRatio":11.73,"shortFloat":15.03023,"daysGap":18.34,"name":"QS"},{"volume":14397838,"price":14.3,"change":29.53,"float":89476163,"relativeVolume":597.0198431094809,"shortRatio":12.8,"shortFloat":50.04126,"daysGap":1,"name":"PCT"},{"volume":8587131,"price":13.82,"change":10.91,"float":108788732,"relativeVolume":584.1824156088004,"shortRatio":3.04,"shortFloat":3.32353,"daysGap":18.14,"name":"LC"},{"volume":269585,"price":8.0965,"change":15.5,"float":1385027955,"relativeVolume":1879.6890252405522,"shortRatio":1.43,"shortFloat":0.00205,"daysGap":-1.71,"name":"IONR"},{"volume":216922,"price":10.78,"change":21.81,"float":189571070,"relativeVolume":560.5219638242894,"daysGap":10.17,"name":"NVA"}]

```

The biggest problem right now is the fact the filtering (jsonata expression) is not producing the expected set of records. Initially I thought they might have been missing in the input data, but as I mentioned I proceeded to copy/paste (directly from debug node) the raw data into excel and confirmed that those records existed. Have no idea what's going on there and how to troubleshoot.

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [25 October 2024 18:01 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/24 "2024-10-25T18:01:46Z")

</div>

P.S. Just tried sending the converted data to file node, but instead of 5527 records the text file only contained two; I ran it several times, confirming in the debug console that the data going into the file node looked like this (so another mystery):

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

---

<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 October 2024 18:10 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/25 "2024-10-25T18:10:29Z")

</div>

> [@sonificator](#):
>
> Still confused, I did provide the example of how I wanted to filter the data (conditional values) - in fact, you provided the jsonata expression to do that ?

No you provided an input object and the conditional logic, not the values.  
So my OG expression filtered the object. You never provided an example showing you wanted an array with the NVDA etc value added.  
You then said you wanted the objects property (NVDA etc) added to the object and return an array. I then pointed out you never gave us a output example in your OG post, which would of eliminated the assumptions we had to make.

I then provide this

> [@E1cid](#):
>
> ```auto
> [$each(
> $.payload.data.data, 
> function($v,$k){
> $v.change > $.change and $v.price > $.price_min and $v.price < $.price_max ?
> $merge([{"stock":$k}, $v])
> }
> )]
> 
> ```

Which is an example of of adding the NVDA to the object and returning an array.

Example of how best to provide info that reduces assumptions when we reply.

Input

```auto
{
    "status": 200,
    "data": {
        "data": {
            "AAPL": {
                "volume": 18457363,
                "price": 230.42,
                "change": -0.15,
                "float": 15179469955,
                "relativeVolume": 36.30859039199602,
                "shortRatio": 2.32,
                "shortFloat": 0.93377,
                "daysGap": -0.34,
                "averageVolume": 39185859.6,
                "rsi": 52.096
            },
            "NVDA": {
                "volume": 130080525,
                "price": 139.7328,
                "change": 0.12,
                "float": 23541535433,
                "relativeVolume": 40.00929295738587,
                "shortRatio": 0.8,
                "shortFloat": 1.06658,
                "daysGap": 0.9,
                "averageVolume": 254269218.6,
                "rsi": 62.832
            }
        }
    }
}

```

conditionals

```auto
change > $$.change and price > $$.price_min and price < $$.price_max

```

Value for output  
change \> 0  
price\_min 100  
price\_max 300

expected output

```auto
[
    {
        "stock": "NVDA",
        "volume": 130080525,
        "price": 139.7328,
        "change": 0.12,
        "float": 23541535433,
        "relativeVolume": 40.00929295738587,
        "shortRatio": 0.8,
        "shortFloat": 1.06658,
        "daysGap": 0.9,
        "averageVolume": 254269218.6,
        "rsi": 62.832
    }
]

```

Remember we volunteer our time, so the clearer you make the question the less time we waste.

---

<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 October 2024 18:27 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/27 "2024-10-25T18:27:08Z")

</div>

> [@sonificator](#):
>
> potentially missing data still to be figured out

This has been answered, there is no missing data the debug limits its display, when you copy the value it is all there.

> [@sonificator](#):
>
> However the output from expression you just posted (that adds stock symbol and outputs an array) is blank.

More info is needed as working for me with the input you provided.

 ![chrome_screenshot_25 Oct 2024 19_26_10 BST](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d36572283fdb6d523e1e4963c145daa0bf6f656a.png)

```auto
[{"id":"4ad42215ec3280f3","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"status\": 200, \"data\": { \"data\": { \"AAPL\": { \"volume\": 18457363, \"price\": 230.42, \"change\": -0.15, \"float\": 15179469955, \"relativeVolume\": 36.30859039199602, \"shortRatio\": 2.32, \"shortFloat\": 0.93377, \"daysGap\": -0.34, \"averageVolume\": 39185859.6, \"rsi\": 52.096 }, \"NVDA\": { \"volume\": 130080525, \"price\": 139.7328, \"change\": 0.12, \"float\": 23541535433, \"relativeVolume\": 40.00929295738587, \"shortRatio\": 0.8, \"shortFloat\": 1.06658, \"daysGap\": 0.9, \"averageVolume\": 254269218.6, \"rsi\": 62.832 } } } }","payloadType":"json","x":210,"y":7700,"wires":[["29b7a996ff9053cc"]]},{"id":"29b7a996ff9053cc","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"change","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"price_min","pt":"msg","to":"100","tot":"num"},{"t":"set","p":"price_max","pt":"msg","to":"300","tot":"num"},{"t":"set","p":"payload","pt":"msg","to":"[$each(\t $$.payload.data.data, \t function($v,$k){\t $v.change > $$.change and $v.price > $$.price_min and $v.price < $$.price_max ?\t $merge([{\"stock\":$k}, $v])\t }\t)]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":7700,"wires":[["4e5340ffce155365"]]},{"id":"4e5340ffce155365","type":"debug","z":"d1395164b4eec73e","name":"debug 2580","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":7700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [25 October 2024 18:27 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/28 "2024-10-25T18:27:11Z")

</div>

I understand (doing my best). Your last jsonota expression did exactly what I was looking for. Thanks so much for your time.

---

<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:** [8 November 2024 18:27 UTC](https://discourse.nodered.org/t/jsonata-array-filtering/92640/29 "2024-11-08T18:27:28Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/jsonata-array-filtering/92640.md?page=1)
