# Filter on (nested) relationship attribute?

**URL:** <https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275>\
**Category:** Uncategorized\
**Created:** [April 24, 2018, 7:42pm UTC](https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275 "2018-04-24T19:42:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marceloverdijk](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/marceloverdijk/32/521_2.png) [@marceloverdijk](https://discuss.jsonapi.org/u/marceloverdijk)\
**Post date:** [April 24, 2018, 7:42pm UTC](https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275/1 "2018-04-24T19:42:07Z")

</div>

Keeping in mind that the jsonapi spec clearly notes that it is agnostic about filtering strategies,  
the recommended section provides some help regarding implementing search:

> Furthermore, multiple filters can be applied to a single request:
> 
> `GET /comments?filter[post]=1,2&filter[author]=12`

What would be recommended way to filter on attributes of the relationships like `author.name`?

Personally I don’t like all the square brackets so I would prefer something like `filter[author.name]=John`,  
but looking at the rest of the spec probably something like `filter[author][name]=John` is closer.  
In this forum I also saw examples like: `filter[author:name]=John`.

What do you think is best, and would it be a good idea to document this in the recommended section?

Another I’m wondering is how to filter on nested relationships. Image the author has a `nationality` relationship with a `alpha2-code` attribute.  
Filtering on this using dots could look like:

Using dots this could look like `filter[author.nationality.alpha2-code]=NL`.

Using square brackets it could look like: `filter[author][nationality][alpha2-code]=NL`.

What are your opinions?

---

<div class="post-metadata">

**Author:** ![marceloverdijk](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/marceloverdijk/32/521_2.png) [@marceloverdijk](https://discuss.jsonapi.org/u/marceloverdijk)\
**Post date:** [April 26, 2018, 8:35pm UTC](https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275/3 "2018-04-26T20:35:50Z")

</div>

Thinking about this again. For inclusion of related resources the spec mentions:

> The value of the include parameter MUST be a comma-separated (U+002C COMMA, “,”) list of relationship paths. A relationship path is a dot-separated (U+002E FULL-STOP, “.”) list of relationship names.

Meaning a _dot_ should be used to form a relationship path.  
In that case I think it would also be good to use a _dot_ in the filter field paths.

So probably `filter[author.name]=John` is the most consistent as well.

---

<div class="post-metadata">

**Author:** ![scott.werner.vt](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/scott.werner.vt/32/297_2.png) [@scott.werner.vt](https://discuss.jsonapi.org/u/scott.werner.vt)\
**Post date:** [May 1, 2018, 1:49pm UTC](https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275/4 "2018-05-01T13:49:20Z")

</div>

I would also recommend including the operator on the filter:

- `filter[author.name][eq]=John`
- `filter[author.name][not-eq]=John`

---

<div class="post-metadata">

**Author:** ![marceloverdijk](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/marceloverdijk/32/521_2.png) [@marceloverdijk](https://discuss.jsonapi.org/u/marceloverdijk)\
**Post date:** [May 1, 2018, 2:07pm UTC](https://discuss.jsonapi.org/t/filter-on-nested-relationship-attribute/1275/5 "2018-05-01T14:07:38Z")

</div>

Yes the operator like `filter[author.name][eq]=John` could be a good extension.  
With only `filter[author.name]=John` meaning an implicit `[eq]`.
