0

I'm trying to write a very simple JSON API query on Drupal 8: return all users except uid 0 and 1. Sounds trivial enough but I haven't been able to figure it out and to find any example.

Something like this?

jsonapi/user/user?filter[drupal_internal__uid]!=0

Any ideas would be most welcome!

Hubert
  • 369
  • 3
  • 21

1 Answers1

2

Something like this should work as you want:

/jsonapi/user/user?filter[user-uid][condition][path]=uid&filter[user-uid][condition][operator]=NOT IN&filter[user-uid][condition][value][]=0&filter[user-uid][condition][value][]=1

For more informations: https://www.drupal.org/node/2943641

Edit: If you use cURL, brackets will throw an error, add "--globoff" to your request.

Example:

curl --globoff --location --request GET "{{localhost}}/jsonapi/node/article?sort=nid&page[limit]=10&filter[article-title][path]=title&filter[article-title][value]=headless&filter[article-title][operator]=NOT%20IN".

Reference: https://stackoverflow.com/a/8333999/5052969

potassium
  • 36
  • 4
  • Thanks for the hint! What I don't get is that a lot of the syntax is not a valid URL. So if I use curl or something to call an endpoint, these queries are not accepted as a proper URL. For example, instead of "NOT IN", shall I write "NOT%20IN"? (Which doesn't seem to work) – Hubert Jun 02 '20 at 21:48
  • 1
    Yes it's a lot of syntax, I don't know if there is an alternative to this. Concerning your problem it worked for me with this: curl --globoff --location --request GET "{{localhost}}/jsonapi/node/article?sort=nid&page[limit]=10&filter[article-title][path]=title&filter[article-title][value]=headless&filter[article-title][operator]=NOT%20IN". The problem could be the brackets (this is why I used "--globoff") link: https://stackoverflow.com/a/8333999/5052969 – potassium Jun 03 '20 at 06:42
  • The drupal documentation (and JSON:API specification) writes URLs before encoding them for better readability. You should encode special characters like `[`, `]`, whitespaces and so on before using them in a URL. If you don't want to do that encoding by hand, you can use `--data-urlencode` option from cURL. – jelhan Jun 08 '20 at 06:47
  • Is there any official documentation from json-api that talks about filtering apart from the below: https://jsonapi.org/format/#fetching-filtering. It doesn't provide any details as the drupal's documentation. – H S Raju Mar 31 '21 at 03:12