Filtering JSON Data

Thanks to @jan and @RicardoE105 for their help on other posts that have got me this far!
I’m just a little stuck with using a function to filter JSON data. I’m able to get data into N8N, here’s an example:

{
  "id": 1953,
  "title": {
    "raw": "Test Post 1", 
    "rendered": "Test Post 1" 
  },
  "slug": "test-post-1",
  "link": "https://mywebsite.com/test-post-1/", 
  "status": "publish", 
  "type": "category_2", 
  "author": 1, 
  "date": "2020-10-20T10:20:45", 
  "date_gmt": "2020-10-20T09:20:45", 
  "modified": "2020-12-17T02:41:48", 
  "modified_gmt": "2020-12-17T01:41:48", 
  "content": {
     ... and so on

What I really want to do is filter these posts by their modified date from today.
I.e. “Today - 180 days”
I’m running a cron job every day, so posts that are 180 days old will trigger an action

Thanks in advance! This app rocks!

Hey @ljcn, glad to hear that you are enjoying n8n and that you found the other posts helpful :slight_smile:

You can accomplish that using a Function node. You can subtract the modified date from the current date to check if that is greater than 180 days. I created a sample workflow that showcases how to do that.

In this workflow, you’ll notice that I use the first Function node to simulate the data that you posted above (simplified version). In the second Function node, I convert the incoming date to milliseconds for subtraction with today’s date. I then convert the milliseconds into days and check if that is older than 180 days. If it is, the record will be passed on as input to the next nodes. If not, this node will not send any data to the next nodes.

const newItems = [];
const olderThan = 180;

for(let i=0; i<items.length; i++) {
  if(parseInt((new Date() - new Date(items[i].json.date))/(1000*60*60*24)) > olderThan) {
    newItems.push({json: items[i].json});
  }
}

return newItems;

For you use-case, you might have to adjust the code in the function node a bit to point to modified or modified_gmt instead of date. I hope that helps :slight_smile:

1 Like

Thanks @tanay!
Unfortunately I’m still struggling :cry:
I’ve tried changing the parameters slightly from date to modified but it still doesn’t receive anything.
I noticed your example uses ‘json’ before each object. I’m using a HTTP request in n8n to pull JSON, and it returns looking like this:
image

I’ve tried also just returning items based on their ID or another parameter but I’m doing something drastrically wrong

1 Like

Hey @ljcn. Are you at liberty to share the URL you are sending a request to using the HTTP Request node? If you’d rather not share it publicly, you can also send that to me as a DM. I can then take a look :slight_smile:

1 Like

Are you sure there are any older than 180 days? Because if there are none, that could also explain why it does not work. So you can test that by changing the 180 to something shorter like 1 to see if that is the problem.

1 Like

Thanks both for your help! Really appreciate it

@tanay I’ve send the link via DM
@jan I did try changing to 1 day but I don’t understand javascript enough - I’ve spent the last few weeks learning and now my head hurts lol

Also thinking, my particular case use is to run a cron every 24 hours, find posts 180 days old and then notify the team the post needs updating. I’m guessing I should really try to include posts 180-181 days (rather than more than 180 days) otherwise we’d keep getting duplicates until the post is modified.

1 Like

Hey @ljcn, thanks for sending me the URL. It seems that the example workflow did not work for you because the incoming data structure from the API differed from the JSON that you originally shared. I have modified the code so that it should work for you now.

const newItems = [];
const olderThan = 10;

for(let i=0; i<items.length; i++) {
  for(let j=0; j<items[i].json.length; j++) {
    if(parseInt((new Date() - new Date(items[i].json[j].modified))/(1000*60*60*24)) > olderThan) {
      newItems.push({json: items[i].json[j]});
    }
  }
}

return newItems;

You can paste this code in a Function node after the HTTP Request node. Note that I have changed the value of the olderThan variable to 10, so that you can test it out with the API. You might want to change it back to 180. With regard to the 180-181 condition, you can specify that in the if() part of the code. Hope that helps :slight_smile:

2 Likes

Oh I see what you mean!
Thanks once again for your support, does N8N have a donations page?
I also managed to get the 180-181 if statement working, have modified the parameters in the database to test and it’s working great! Thanks again!!! :smiley:

1 Like

That’s great to hear @ljcn! We currently do not take any monetary donations. However, being a product with a strong focus on community, there are a some contributions that we very valuable for us that we’ve outlined in our How can I contribute? page.

We also have a cloud offering n8n.cloud that makes it easier for people to get up and running with n8n :slight_smile: