Search code examples
jquerystrapi

Filtering Array on API Request with Strapi


I am trying to build a request with Strapi filtering the json response, depending on a string contained in an array of options. Here is the Json :

{
"data": [
    {
        "id": 1,
        "attributes": {
            "createdAt": "2023-03-16T10:32:35.724Z",
            "updatedAt": "2023-04-07T15:05:15.046Z",
            "publishedAt": "2023-03-16T10:33:04.365Z",
            "TitleSupplier": "Catalogue Casio",
            "GammeSupplier": "Fournitures Scolaires",
            "MarqueSupplier": "Casio",
            "Couleur": "#DB3938",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires",
                "Fournitures de Bureau"
            ],
            "CouleurGamme": null
        }
    },
    {
        "id": 2,
        "attributes": {
            "createdAt": "2023-03-17T14:16:19.454Z",
            "updatedAt": "2023-04-07T15:03:31.486Z",
            "publishedAt": "2023-03-17T14:16:29.716Z",
            "TitleSupplier": "Catalogue Canson",
            "GammeSupplier": "Fournitures Scolaires",
            "MarqueSupplier": "Canson",
            "Couleur": "#DB3938",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires"
            ],
            "CouleurGamme": null
        }
    },
    {
        "id": 5,
        "attributes": {
            "createdAt": "2023-03-30T10:54:00.894Z",
            "updatedAt": "2023-04-07T15:04:54.465Z",
            "publishedAt": "2023-03-30T10:54:01.982Z",
            "TitleSupplier": "Catalogue Apli-Agipa",
            "GammeSupplier": "Fournitures Scolaires",
            "MarqueSupplier": "Apli-Agipa",
            "Couleur": "#DD4343",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires",
                "Fournitures de Bureau"
            ],
            "CouleurGamme": null
        }
    },
    {
        "id": 6,
        "attributes": {
            "createdAt": "2023-04-07T11:27:54.716Z",
            "updatedAt": "2023-04-07T15:04:44.908Z",
            "publishedAt": "2023-04-07T11:28:03.198Z",
            "TitleSupplier": "Catalogue Ancor",
            "GammeSupplier": "Fournitures Scolaires",
            "MarqueSupplier": "Ancor",
            "Couleur": "#3498A8",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires",
                "Fournitures de Bureau"
            ],
            "CouleurGamme": "#3498A8"
        }
    },
    {
        "id": 7,
        "attributes": {
            "createdAt": "2023-04-07T14:29:56.349Z",
            "updatedAt": "2023-04-07T15:05:04.770Z",
            "publishedAt": "2023-04-07T14:29:58.437Z",
            "TitleSupplier": "Catalogue CEP",
            "GammeSupplier": "Fournitures de Bureau",
            "MarqueSupplier": "CEP",
            "Couleur": "#3498A8",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires",
                "Fournitures de Bureau"
            ],
            "CouleurGamme": null
        }
    },
    {
        "id": 8,
        "attributes": {
            "createdAt": "2023-04-07T14:38:21.485Z",
            "updatedAt": "2023-04-07T15:06:22.568Z",
            "publishedAt": "2023-04-07T14:38:22.596Z",
            "TitleSupplier": "Catalogue Conquérant",
            "GammeSupplier": "Fournitures Scolaires",
            "MarqueSupplier": "Conquérant",
            "Couleur": "#3498A8",
            "Annee": "2023",
            "Gamme": [
                "Fournitures Scolaires",
                "Mobilier de Bureau"
            ],
            "CouleurGamme": null
        }
    }
],
"meta": {
    "pagination": {
        "page": 1,
        "pageSize": 25,
        "pageCount": 1,
        "total": 6
    }
}

}

I am trying to create a request that says : Please send me all the catalogs for which the "Gamme" equals to "Mobilier de bureau".

Here is the requests i have tried so far :

 http://localhost:1337/api/supplier-catalogs?filters/[Gamme][$in][0]=Mobilier de Bureau


http://localhost:1337/api/supplier-catalogs?filters/[Gamme][$in]=Mobilier de Bureau

http://localhost:1337/api/supplier-catalogs?filters/[Gamme][$eq]=Mobilier de Bureau

This was the most obvious options while i was reading the doc but all of them keep sending me all the array instead of the filtered query.

Thanks for the help,


Solution

  • Following antokhio's comment, I am posting the solution to the answer. Strapi by itself doesn't provide the data like this:

    {
      "Gamme": [
        "Fournitures Scolaires",
        "Fournitures de Bureau"
      ]
    }
    

    But, there is a plugin called "Multi Select" that can provide this type of JSON Answer. It was the one I was using. To be able to create the good request I used the "Contains" instead of "Eq" filter operator, like this:

    http://localhost:1337/api/supplier-catalogs?filters[Gamme][$contains]=Mobilier de bureau&sort[0]=TitleSupplier&populate=*
    

    Thanks,