anand
8 October 2022 05:52
1
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:
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
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.
anand
8 October 2022 08:38
3
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' )
anand
8 October 2022 08:52
5
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
E1cid
8 October 2022 08:57
7
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
anand
8 October 2022 09:01
8
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?
anand
8 October 2022 09:05
9
My node-red version is -
NODE_RED_VERSION v3.0.2
NODE_VERSION 16.16.0
Colin
8 October 2022 09:10
10
Please feed the message containing the array into a debug node and screenshot what it shows.
anand
8 October 2022 09:14
11
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
Colin
8 October 2022 09:17
12
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
anand
8 October 2022 09:24
13
Thanks, This works perfectly !!
I am sorry, I should have phrased it better. I am still learning the ropes for building some personal projects.
system
Closed
22 October 2022 09:24
14
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.