Return only specific fields from the API GET response

AI-generated summary

The discussion revolves around whether it’s possible to specify that the Discourse API response to a GET request returns only the requested field. gassim asked if there’s a way to make the response return only a specific field, instead of the whole response. blake replied that this is not an option in the core app, but suggested filtering the results using tools like jq or programming languages. Alternatively, blake recommended using the data explorer plugin to create a query that returns only the needed data and then making an API request to that query. gassim found this suggestion helpful and thanked blake for the solution.

Continuing the discussion from Discourse API Documentation:

Hi

I couldn’t find an answer to this, please. Does anyone know if it’s possible to specify that the Discourse API response to a GET request returns only the requested field?

For example, the GET request to List group members (https://{defaultHost}/groups/{id}/members.json) returns:

 {
   "members": [
     {
       "id": 0,
       "username": "string",
       "name": "string",
       "avatar_template": "string",
       "title": "string",
       "last_posted_at": "string",
       "last_seen_at": "string",
       "added_at": "string",
       "timezone": "string"
     }
   ],
   "owners": [
     {
       "id": 0,
       "username": "string",
       "name": "string",
       "avatar_template": "string",
       "title": "string",
       "last_posted_at": "string",
       "last_seen_at": "string",
       "added_at": "string",
       "timezone": "string"
     }
   ],
   "meta": {
     "total": 0,
     "limit": 0,
     "offset": 0
   }
 }

Is there a way to make the response return only a specific field/? For example, to return only the member’s usernames?

{
  "members": [
    {
       "username": "string"
    }
  ]
}

I know it’s possible to retrieve the whole response and then use coding to filter the JSON and the arrays in order to get a specific value; however, I want the request to return the specific value instead. Is this possible?

Thank you!

Great question, but no this is not an option. There is not anything in the core app that is listening for params to filter by.

I think you will be best off just filtering out the results either with a tool like jq on the command line or using whatever programming language you are using to make the api requests.

If this is indeed a must have requirement two options do exist though. You could use the data explorer plugin with a query that returns only the data you need and then make an api request to that query. Another alternative would be to create a custom plugin that you create a new route and controller for that returns only the data you need.

Amazing! I think this will help a lot. :folded_hands: Thank you so much!