json-server icon indicating copy to clipboard operation
json-server copied to clipboard

It is not possible to filter based on collection property.

Open hamidmayeli opened this issue 2 years ago • 2 comments

Given the following data:

[
    {
        "id": 2,
        "rate": 5,
        "logo": "https://loremflickr.com/640/480?lock=6423556804050944",
        "email": "[email protected]",
        "telephone": "021 178 03 73",
        "categories": [
            {
                "id": 1,
                "name": "cat 1"
            },
            {
                "id": 2,
                "name": "cat 2"
            }
        ]
    }
]

Is it possible to filter based and category having a specific id.

I have tried http://localhost:4000/business?categories.id=10 and it didn't work.

hamidmayeli avatar Sep 13 '23 19:09 hamidmayeli

First, only objects are supported and second, if you want to filter based and category having a specific ID. then you can create categories as separate objects and link them to business objects like { "business": [ { "id": 2, "rate": 5, "logo": "https://loremflickr.com/640/480?lock=6423556804050944", "email": "[email protected]", "telephone": "021 178 03 73" } ], "categories": [ { "id": 1, "name": "cat 1", "businessId": 1 }, { "id": 2, "name": "cat 2", "businessId": 2 } ] } now you can get categories http://localhost:3000/categories?id=2 and you can also get specific business object
http://localhost:3000/categories?businessId=1

sahil-arora-76 avatar Nov 17 '23 19:11 sahil-arora-76

Given the following data:

[
    {
        "id": 2,
        "rate": 5,
        "logo": "https://loremflickr.com/640/480?lock=6423556804050944",
        "email": "[email protected]",
        "telephone": "021 178 03 73",
        "categories": [
            {
                "id": 1,
                "name": "cat 1"
            },
            {
                "id": 2,
                "name": "cat 2"
            }
        ]
    }
]

Is it possible to filter based and category having a specific id.

I have tried http://localhost:4000/business?categories.id=10 and it didn't work.

The reason your query http://localhost:4000/business?categories.id=10 didn't work is that json-server doesn't support filtering by properties of nested objects or arrays, like the categories array in your data, using its default query parameters. It only supports filtering on top-level fields directly.

MadhuSaini22 avatar Dec 26 '23 06:12 MadhuSaini22