Using variable in jsonb

I'm trying to use a variable in my query, my database is postgresql and this is the line that doesn't work:
WHERE tags @> ANY (ARRAY [concat('''["', {{tag}}, '"]''')]::jsonb[])
this works when I don't use variable:
WHERE tags @> ANY (ARRAY ['["Y"]','["X"]']::jsonb[])
I've got this error:
ERROR: invalid input syntax for type json Detail: Token "'" is invalid. Where: JSON data, line 1: '...
I appreciate your help!

Hi @ghazalak
So if we break down what you're generating in your concat, then this fails:
'''["', {{tag}}, '"]'''
While this should work:
'["', {{tag}}, '"]'
When working with variables and having problems, then replace the variable with hardcoding, so it makes it easier to figure out if the problem comes from the variable or incorrect syntax.

1 Like

Thanks, Flamber, it works!