How to query JSON?

Is it possible to directly search for a given key value pair in a JSON field?

e.g.

//services.js
module.exports = {
  findProductInStock(size) {
    return strapi.query("product").find({ sizes: {size: true} });
  },
};

where sizes is a json field of sizes with boolean values.

At this time it’s not possible to filter on JSON fields as not all databases support native JSON filtering. You can kinda get away with it by using a _contains filter but it can be quite ugly.

Is it possible by using knex/bookshelf methods?

If not, what is the best way to have a collection of products, each with multiple sizes that are either in or out of stock, then have a query fetching all products that are in stock for a given size. By having many enums on each products?

Relations would be the best way

1 Like