Check if collection exist and get the first value available

Hi everyone,

I’ve been stuck for few days with this problem. And unable to find an answer neither from the community nor from Make Help Center or AI.

Context:
I have an HTTP module that give me 2 possible output, one containing a “rain” Collection, the other one not.

Objective:
I’d like to set a variable:
IF “rain” EXISTS THEN get the first value
ELSE 0

Examples:
Here are 3 examples of possible output from the HTTP module:

#1 - “Rain” exist and first value = 1

#2 - "Rain doesn’t exists so value = 0

#3 - “Rain” exist and first value = 0.32

I’ve tried a lot of combination with contains, map, get, toArray, etc. But nothing works…

Some ideas ? :pray:

Hello @Arthur_AdL,
Welcome back.

Can you please share all that multiple request data in JSON or TXT files? Then I can try it on my end.

These all things are based on keys only. There is no other way to solve this.

The process looks like this.

→ First check and see if that rain key exists.
→ Then go get the very first item of it. But here is another twist, that rain element also has different keys and not sure which exists
→ Need to cross-check which key exists and then get that value.

Modules and features need to be used.

→ Array OR JSON Aggregator
→ Iterator
→ Filters
→ GET & MAP function
→ Regular Expression(if Needs)


:bulb:P.S.: Always search first, Check Make Academy. If this is helpful, Mark Best as Solutions :white_check_mark: and give :+1:
If you need expert help or have questions? Contact or comment below! :point_down:

Welcome to the Make community!

You can use the built-in function ifempty

e.g.:

{{ ifempty(3.data.data.1.rain.`1h`; 0) }}

For more information, see the function documentation in the Help Center and do the tutorials in the Make Academy.

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

Hi samliew,

Thanks a lot for the reply. It almost worked. But as @dilipborad (thanks also for the reply) mentioned, it misses the fact that the key can be ‘1h’ or ‘3h’.

I manage to solve it with:

{{ifempty(3.data.data.1.rain.`1h` | 3.data.data.1.rain.`3h`; 0)}}

:pray: :confetti_ball:

2 Likes