Return string from an array

I’m a beginner here and I have a question…

I have the following structure

{
“tags”:
[
“cat_access”,
“tip_escl”
]
}

I would like to return only the value that starts with “cat_”, so I built the following expression:

{{ $jmespath($json.tags, "[?contains(@, ‘cat_’)] ") }}

however, the value returned was:

[Array: [“cat_acess”]]

and I would like to return just the value:

cat_access

I tried with the value:

{{ $jmespath($json.tags, “[?contains(@, ‘cat_’)]{tags: tags[0]}”) }}

however it returned empty.

Hey Allan,

I don’t know jmespath actually. My suggestion is to solve it with plain javascript.
So this {{ $json.tags.filter(tag => tag.startsWith(‘cat_’)).first() }} would be the substitution for {{ $jmespath($json.tags, "[?contains(@, ‘cat_’)][] ") }}. With the .first() you’ll get the first element with starts with cat_. This will solve your problem so far.

Cheers
Nico

2 Likes

Hi,

{{ $jmespath($json.tags, “[?contains(@, ‘cat’)]”)[0] }}

Jos

Firstly, sorry for the delay in responding.
Regarding your answer, thank you, that’s exactly what I needed, however, I have a problem. When there is no result it is returned as “undefined”, is it possible to handle this? that is, if it doesn’t find any results, does it return a default value?

{{ $jmespath($json.tags, “[?contains(@, ‘cat’)]”)[0] ?? ‘default_value’ }}

2 Likes

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