Optional Variables

I have data in this format

[
{
"title": "Start",
"time": "0:00"
},
{
"title": "Feudal Age",
"time": "0:36"
},
{
"title": "Castle Age",
"time": "0:52"
}
]

And i’d like to print a simple statement, like

Start Time was: 0:00
Feudal Age Time was 0:36
Castle Age Time was 0:52

However sometimes, there is no Castle Age time. So using something like this gives us an error:

echo Castle Age Time was {{ $item("2").$node["Code"].json["time"] }}

In cases like these I normally use something like this as a fallback:

{{ $item("2").$node["Code"].json["time"] ?? "Not Found"}}

But this doesn’t work!

Any advice on what I should do?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @UnluckyForSome , what you are looking for is Optional chaining (?.).

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