Match a value in the base level of an array of objects and return another value in the nested object

:bullseye: What is your goal?

get matching value from an HTTP response

:thinking: What is the problem & what have you tried?

in an HTTP call I receive the following reply:

{
	"status": "success",	
	"order_items": [
		{
			"id": "460822898",			
			"metadata": {
				"alternate_id": "132488474145081"				
			}
		},
		{
			"id": "460822899",			
			"metadata": {
				"alternate_id": "132467704472561"				
			}
		}
	]
}

There are other fields in each of the collections but I’ve removed them. I want to get the corresponding alternate_id when I use the id as an input. So inputting 460822898 should return 132488474145081. The id:alternate_id pairing is always unique.

I can get it to work by using an iterator after the HTTP module, and filter the id value = 460822898, then using get(metadata;alternate_id)

Is it possible to accomplish the same by using a formula? I’ve already got 3 layers of iterators and I’d like to avoid doing that if possible.

:camera_with_flash: Screenshots (scenario flow, module settings, errors)

Hello @skanfklwef,

It looks like the map() and get() function would help you out here. In the map() function you can pass a (complex) array and return a new array with only the values that match a certain key. With the get() function, you can get the first value of that new array, essentially converting it to a single value.

Here is more info about mapping arrays

map([array];value_that_you_want;raw_key;key_to_filer)

In this case, map from the order items array the id where metadata.alternate_id equals 132488474145081. Then get the first value using get() or first()

Hope this nudges you in the right direction.

Cheers,
Henk

4 Likes

@Henk-Operative Thank you so much for the hint. I didn’t know the map() function could filter like that.

{{first(map(1.data.order_items; “metadata.alternate_id”; “id”; 460822899))}}

It’s a lot simpler than what I did, you are amazing!

1 Like