Filter a query on a JSON field

I followed the guide on Wappler Documentation - Inserting Tagify Values into a Database and that worked so I now have a JSON field with the correct values.

image

I am now trying to filter a query using the values in the JSON field but without success. I am aware that I have to use “IN” and I have tried with and without split but no success. I have the filter parameter set as an array, should this be an object?

The parameter shows in dev tools that it is passing the correct values but I seem to be going round in circles, what am I doing wrong ?
image

Thanks

@Teodor any advice you can offer?

hey @elpod,

You are trying to check if the json values are in the filter array…

So, if kb_tagged contains “2, 5, 8” you want to check if any of these values (2 or 5 or 8) are in the filter array ?
Or all of the values (2 and 5 and 8) are in the filter array?

Please think of what you are trying to do and tell us so we can answer accordingly

Hi

Yes, I want to check if any of the values (2 or 5 or 8) are in the filter array

Thanks

then you need a repeat (or while) in order to check this…
Give me 5’ to make a sample for you

ok…
Took me a little more than 5’… :rofl: :rofl: :rofl:

Here is a screenshot of the serverAction

I’m gonna write you the steps

I have worked on my table “products” that contains a json column (prd_Tags).

  1. I pull all the records of my products table (NO OUTPUT)
  2. Create an arrayList and set the value to my step-1 query
  3. Clear the arrayList (I just wanted to copy the schema, we don’t need the data)
  4. I add a repeat with expresion the step-1 query (loop throught the table’s products)
  5. Set Value tags_Array and gave it the current repeat’s prd_Tags column value
  6. I create a switch and set it to false (this is to check if the record is already added to the arrayList)
  7. I add inside the repeat one more repeat to repeat through each product’s tags (JSON)
    (here I explain that you could skip step 5 and set now the repeat straight to the prd_Tags column, but just like to see the setValue steps and know what I’m doing)
  8. I add a condition to check when to add the current product to the arrayList.
    If switch == false (the product has not been already added to arrayList) and $_GET.filtertags,contains($value) (filter contains the current product’s tag)
  9. if the conditions are met add the current repeat’s product to the arrayList
  10. set the switch to true in order not to add again the same product
  11. Closing all all repeats, we get the arrayList and this is the final filtered query that we are going to use client-side to list our products
    (THIS IS THE ONLY STEP/VALUE THAT HAS OUTPUT ticked)

Now I realized that if no filter is set the arrayList(new query) will be empty…

You can avoid this and return all the records of the table if you add a condition after step-2 and check if filter has value.
Enclose all steps 4-10 inside the if and leave step-11 outside the if statement (so we have in both cases the arrayList to list our records client-side)

I hope it helps

1 Like

Wow, now I don’t feel so bad at not being able to figure it out. Will give this a go.

Thanks for all your help

1 Like