Check if a specific value exists in JSON payload

Describe the issue/error/question

What
I need to check if a payload contains a specific key name but the If node using ‘value is not empty’ doesn’t seem to be able to perform this function.

Why
I want to create a todoist task (with a due date) from a newly created google calendar event. But Google returns two different JSON payloads for new calendar events:

  1. A payload that contains a dateTime key/value pair for events with a date and time.
  2. A payload that contains a date key/value pair for for events with only a date (ie. all day/multi day events)

I want to be able to determine if the payload contains ‘dateTime’ or just ‘date’ and then create a todoist task using the dateTime or ‘date’ value as a due date.

JSON Payload for event containing time

[
  {
    "kind": "calendar#event",
    "etag": ""33435481571758111"",
    "id": "5p3tk2345ytdid727u9ebo3b2sdb",
    "status": "confirmed",
    "htmlLink": "https://www.google.com/calendar/event?eid=N112325433566899885678",
    "created": "2022-12-05T11:46:25.000Z",
    "updated": "2022-12-05T11:46:25.879Z",
    "summary": "This is my test event",
    "creator": {
      "email": "[email protected]",
      "self": true
    },
    "organizer": {
      "email": "[email protected]",
      "self": true
    },
    "start": {
      "dateTime": "2022-12-14T22:00:00+10:00",
      "timeZone": "Australia/Brisbane"
    },
    "end": {
      "dateTime": "2022-12-14T22:30:00+10:00",
      "timeZone": "Australia/Brisbane"
    },
    "iCalUID": "[email protected]",
    "sequence": 0,
    "reminders": {
      "useDefault": true
    },
    "eventType": "default"
  }
]

Event that only contain a date

[
  {
    "kind": "calendar#event",
    "etag": ""334345yt63458408000"",
    "id": "2c7pciq54dj23p6s",45ghdsftg
    "status": "confirmed",
    "htmlLink": "https://www.google.com/calendar/event?eid=2345sdfhgbbedrtyrgjfgjhgfhj",
    "created": "2022-12-05T21:34:49.000Z",
    "updated": "2022-12-05T21:34:49.204Z",
    "summary": "Date only",
    "creator": {
      "email": "[email protected]",
      "self": true
    },
    "organizer": {
      "email": "[email protected]",
      "self": true
    },
    "start": {
      "date": "2022-12-14"
    },
    "end": {
      "date": "2022-12-15"
    },
    "transparency": "transparent",
    "iCalUID": "[email protected]",
    "sequence": 0,
    "reminders": {
      "useDefault": false
    },
    "eventType": "default"
  }
]

What is the error message (if any)?

No error message but the if node always returns true even when the dateTime key/value isn’t in the payload

Please share the workflow

Information on your n8n setup

  • n8n version: 0.205.0
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process [own(default), main]: Own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

I think I’ve cracked it!! After bashing together lots of examples, this is where I landed.

The breakthough came when I stumbled on the ‘hasOwnProperty’ method and was able to work out how to target the correct part of the JSON. Below is the where the magic happens:

// Assign the result of the hasOwnProperty() method to a boolean variable
var dateTimeExists = $input.item.json.start.hasOwnProperty("dateTime");
}

return {json:{dateTimeExists}};
2 Likes

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