Set value instead of [Not Found]

Is it possible to set a default value if the value is not found,

In my specific case i need NULL instead of [Not Found] (This spans over multiple columns so making a whole bunch of if statements isn’t gonna do the trick)

Yes, it would be very convenient for me too.
I would like that in case of a value not found, it returns the string [Not Found] and not an empty string.
Maybe we could create a workflow that handles just this situation.
Suggestions?

Thank you

You can do that with a Set-Node and the expression:

{{$json.a || null}}
// or
{{$json.a || "[Not Found]"}}

Assuming the property name is “a”.

Here an example workflow:

4 Likes

Hi Jan, big fan of n8n! Thanks for all the work you do!

I ran into an issue trying to implement this solution and was hoping you knew of a way around this. I am trying to output a string in a SET node if the value is empty, but the example doesn’t seem to work, because even though the output is null, it’s still generating an empty array so the string is not outputted, only the empty null value.

In other words, the {{$json.a.b.c || “Not Found”}} is not outputing the Not Found because in the expression editor it’s showing [Array: []]. Any help is appreciated!

1 Like

Thanks, a lot. That is great to hear!

I assume the problem is that in your code not just c does not exist underneath ‘a.b’, rather also not a.b or not even a at all. That will then throw an error and so the whole expression will not execute, and for that reason the return undefined gets returned.

Example:

  • If you have {a: {b:{}}} and you try to access a.b.c then it will resolve to undefined.
  • If you have {a: {}} and you try to access a.b.c it will throw an error as you try to access c from b which is undefined. Meaning you will try to access c from undefined and that errors.

So if you write {{$json.a.b.c || "Not Found"}} and n8n executes that JavaScript code, that code will error because of the $json.a.b.c part as explained above. The code will only work if the part before || returns to something empty like undefined, null, 0. So you have to make sure in the expression that this is the case for it to work.

I hope that makes sense.

1 Like

It would be nice to have something built in to return some data if nothing is found.
Example use case when using a node to query and API, eg Google people API
if the contact does not exist then nothing is returned, it makes it hard if using an IF node after because the previous node does not exist to run the IF node

Hey @wgicio,

In that case you could tell the node to always output data then in your if set it to a string and use the Is Empty operation. If you always expect an ID from the API you could do if {{$json[“ID”]}} is Empty then on the true you can work on any error logic if needed and anything that goes to the false branch you can process.

Below is a quick example that shows how you could handle it using this method.

Quick Example

2 Likes

Amazing community and big Up to Mr Jon. I have already read thousands of posts answering thousands of questions thanks to your constant answers. Thank you again

3 Likes