How to search in loop inside huge json array objects

Hi

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

[
   {
      "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:

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

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

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.

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.

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

This should work:

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

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.

I am not sure what I missed here.

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

1 Like

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

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

1 Like
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?

My node-red version is -

NODE_RED_VERSION v3.0.2
NODE_VERSION 16.16.0

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

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.

1 Like

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

1 Like

Thanks, This works perfectly !!

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

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