How to exclude a field (or attribute) from REST API response

See image. I want to exclude this field “oldMetaData” from being returned when I access the route “/api/brackets”.

Unfortunately I have spent an hour on this and have not figured out how to do this. Current solutions:

  1. Write a filter that does this. (*I haven’t been able to find the right one)
  2. Do something with lifecycles (not sure what lifecycles are, or how to implement this)
  3. /api/brackets?fields[0]=id&fields[1]=isPublic&fields[2]=name&fields[3]=isComponent&fields[4]=roofType&fields[5]=imageUrl&fields[6]=snow&fields[7]=solarRails&fields[8]=solarPv&fields[9]=utility&fields[10]=oldId&fields[11]=product&fields[12]=ef_tests&fields[13]=ef_profiles&fields[14]=oldEfProfiles&fields[15]=oldProduct&fields[16]=systems_imp&fields[17]=roofs&fields[18]=imp_tests
    3a. This doesn’t work… it breaks after [10], but if I stop at [10] then it works.

Any suggestions would be super welcome!

This topic has been created from a Discord post (1265055895881121855) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

not quite sure but you could try to set ‘private:true’ on the fileld in schema

Hey! Figured it out:


// src/api/bracket/controllers/bracket.js
const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::bracket.bracket', ({ strapi }) => ({
  async find(ctx) {
    const { data, meta } = await super.find(ctx);
    
    // Remove oldMetaData from each item
    const sanitizedData = data.map(item => {
      const { oldMetaData, ...rest } = item.attributes;
      return { ...item, attributes: rest };
    });

    return { data: sanitizedData, meta };
  },

  async findOne(ctx) {
    const { data, meta } = await super.findOne(ctx);
    
    // Remove oldMetaData from the item
    if (data.attributes) {
      const { oldMetaData, ...rest } = data.attributes;
      data.attributes = rest;
    }

    return { data, meta };
  },
}));

thats one way to do it, but the easier way would be to just mark it as private then the default sanitization function would remove it from the response