Share/Propose a Filtering Strategy

We are going to roll this out for the Drupal module that implements JSON API. One thing that I like is that this will not require any parser at all.

In case anyone is interested:

anyone know how we can make price filter with jsonapi ? and what about search filtre also ?
thanks:)
i use many structure but it doesn’t work

Just my 2c but we recently faced the same issue at my job and we ended up going with the following approach suggested above.

filter[{field_name}][{operator}]={compatible_value}
filter[{field_name.nested_field_name}][{operator}]={compatible_value}

where we currently allow for the following operators:

  • eq
  • ne
  • gt
  • lt
  • ge
  • le
  • contains

We found that it’s the closest to the rest of the JSONAPI conventions and felt intuitive to our users.

3 Likes

Hi, just in case if someone looking for something in PHP for server-side, I’m currently working on library which use OData-like filtering, with Doctrine\Collection which supports those logical expressions. There is still a lot of work to integrate at least all of logical operators. But for now it works well with ArrayCollection and for scalar values. Entities are still problem and in the future will be necessary use some QueryBuilder which will create SQL or DQL, depends if you are using doctrine or not. My plan is implement everything what Doctrine Query Builder shares with OData query filter.

If someone is interested in, here is a link of my lib.

P.S. At least you can look at filtering in URI namespace, there is tokenizer, which parse query. It may help to create your own solution.

How about using JSONLogic https://jsonlogic.com/ for filters?

Like ?filter={"and":[{"gte":["line-items.product.price", 100.0]},{"eq":["customer.name", "Rick James"]}]}

For those who go toward such strategy, keep in mind that you are not using the filter query parameter. As an example, if the URL is “http://example.org/foo?filter[id]=1”, then your are rather using the filter[id] query parameter. Your server may treat the square brackets in a particular way, but in reality what you are asking to the consumer of the API is to craft query parameters.

The upcoming v1.1 contains a clarification about this one. It introduces the term filter parameter family to distinguish them from individual query parameters: JSON:API — Specification v1.1 (Still in Development)

As mentioned, elide.io uses RSQL which is a superset of FIQL. jsonlogic is great for post/patch calls, harder for simple GET calls.

We’re using jsonlogic for forms logic and validation and RSQL for advanced filtering (any advanced searches - basically any complex GET call). We really liked what elide.io did with the filtering. Hope this helps.

Is there a specification for RSQL? I wasn’t able to find one from a quick search.

Thanks!

-Fred

1 Like

@fdrake - on the elide site they have a section called “filtering” that kind of discusses it (Elide). They link to a library that has some more description (GitHub - jirutka/rsql-parser: Parser for RSQL / FIQL – query language for RESTful APIs). It’s probably not the definition or specification you were hoping for, but I think it helps.

1 Like

I leave this link to my SDK for some curious code monke. Maybe he will like it. There are OData like filtering strategy and QData (Quadtrodot :: ) strategy. You can steal it if you want :wink: