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