# How to search in loop inside huge json array objects

**URL:** <https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704>\
**Category:** General\
**Created:** [8 October 2022 05:52 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704 "2022-10-08T05:52:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 05:52 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/1 "2022-10-08T05:52:09Z")

</div>

Hi

Very new to node-red, i am trying to get search results from a API that is formatted like so:

```auto
[
   {
      "order_id":"1411969",
      "status":"active",
      "valid_from":"2020-07-14",
      "valid_till":"2022-10-12",
      "common_name":"www.test1.gr"
   },
   {
      "order_id":"1481099",
      "status":"active",
      "valid_from":"2020-07-22",
      "valid_till":"2022-10-20",
      "common_name":"www.test2.gr"
   }
.....
]

```

There are orders: array[427] individual items and each one has another array inside it. please let me know if it's possible to loop through each array and get valid\_till and return all of them on result.

I can get to the first found item in array with @Steve-Mcl answer here:

> [@How get json value from array](https://discourse.nodered.org/t/how-get-json-value-from-array/37626/1):
>
> Good morning, I have a message like this: { "dataflow":[{ "date":"Dec 17, 2020 7:20 AM", "id":2, "cod":27, "value":676 }, { "date":"Dec 17, 2020 7:22 AM", "id":2, "cod":28, "value":670 }, { "date":"Dec 17, 2020 7:25 AM", "id":2, "cod":25, "value":717 }] } I would retrieve and send data if cod==25 (example) with a function node or other node. Ho…

but I want it to loop through all the returned items and get objects that have the correct "valid\_till" date

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 October 2022 06:10 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/2 "2022-10-08T06:10:51Z")

</div>

> get objects that have the correct "valid\_till" date

what is the "valid\_till" date you are looking for ?

In a function node you could do something like

```auto
const results = msg.payload.filter(v => v.valid_till == '<whatever you are looking for>' )
msg.results = results
return msg

```

which will return a filtered result.

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 08:38 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/3 "2022-10-08T08:38:52Z")

</div>

Thank you for the suggestion,

I am a noob at javascript, so please bear with me on this.

the valid\_till will contain a date . Basically, I am getting a JSON array object from which I have to filter the object that contains a particular date.

So from the first example, If I search for a valid\_till date like: "2022-10-12", I should get all objects that have that date inside the object.

```auto
{
      "order_id":"1411969",
      "status":"active",
      "valid_from":"2020-07-14",
      "valid_till":"2022-10-12",
      "common_name":"www.test1.gr"
   }

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 October 2022 08:41 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/4 "2022-10-08T08:41:20Z")

</div>

This should work:

`const results = msg.payload.filter(v => v.valid_till == '2022-10-12' )`

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 08:52 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/5 "2022-10-08T08:52:44Z")

</div>

The solution seems logically correct, but perhaps i am missing some general syntax rules here.

This is the debug alert when I use this solution.

 ![debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a63f5d8aaaf125e1c4d9620aff9df20f03f884c0.png)

I am not sure what I missed here.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 October 2022 08:54 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/6 "2022-10-08T08:54:55Z")

</div>

Sorry, typo: it is `=>` (no space)

---

<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:** [8 October 2022 08:57 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/7 "2022-10-08T08:57:18Z")

</div>

You missed nothing @bakman2 example has/had a typo.

```auto
const results = msg.payload.filter(v => v.valid_till == '2022-10-12' )

```

p.s. always post code as text, it makes it easier for people to copy/edit

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 09:01 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/8 "2022-10-08T09:01:44Z")

</div>

```auto
const results = msg.payload.filter(v => v.valid_till == '2022-10-12' )

```

This gives me:  
function : (error)

"TypeError: msg.payload.filter is not a function"

Is this a default function that is included in node-red?

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 09:05 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/9 "2022-10-08T09:05:37Z")

</div>

My node-red version is -

NODE\_RED\_VERSION v3.0.2  
NODE\_VERSION 16.16.0

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 October 2022 09:10 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/10 "2022-10-08T09:10:06Z")

</div>

Please feed the message containing the array into a debug node and screenshot what it shows.

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 09:14 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/11 "2022-10-08T09:14:17Z")

</div>

![debug_node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a5f8630953bcf55af22e9e4b5c794be50358cf54.png)

I hope this is enough I have expanded the first array object here.

I am trying to get only the objects that have a "valid\_till":"2022-10-12"

as an example.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 October 2022 09:17 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/12 "2022-10-08T09:17:27Z")

</div>

As you can see, msg.payload is not an array. We assumed that the array you were showing us was in the payload. You need  
`const results = msg.payload.orders.filter(v => v.valid_till == '2022-10-12' )`

---

<div class="post-metadata">

**Author:** ![anand](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anand/32/68860_2.png) [@anand](https://discourse.nodered.org/u/anand)\
**Post date:** [8 October 2022 09:24 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/13 "2022-10-08T09:24:01Z")

</div>

Thanks, This works perfectly !!

I am sorry, I should have phrased it better. I am still learning the ropes for building some personal projects.

---

<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:** [22 October 2022 09:24 UTC](https://discourse.nodered.org/t/how-to-search-in-loop-inside-huge-json-array-objects/68704/14 "2022-10-22T09:24:19Z")

</div>

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