Skip to content
Advertisement

Postgresql: How do I use dynamic values when searching jsonb array of objects?

I am currently trying to build a query for finding specfic object within a jsonb array. I have the following query which works fine if I used a hard coded string for the “game” value e.g.

JavaScript

However, if I use a dynamic value like I currently do for username, I get invalid json syntax error. e.g.

JavaScript

How do I search using a dynamic value here? I have done a ton of searching and can’t find any examples. $2 value is just a string so not sure why not accepted.

Advertisement

Answer

When you send this query, it only has ONE parameter:

JavaScript

The correct query is:

JavaScript

You have to make the array with the object in the parameter.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement