Creating a simple list of strings

I’m trying to move from a manually created system over to strapi and so far i’m loving it. One thing I can’t seem to figure out though are lists/arrays of strings/numbers. I was able to create a component that had a single string in it, and then I used that component in a collection (as repeatable). That allowed me to add a list/array of those strings to a collection. The api response with that setup is quite a bit more verbose than it needs to be. For example, let’s say I want to create a playlist of vimeo IDs that is stored in an object as strings. In my current hand-coded json, it looks like this:

    "playOrder":[
        "12342345",
        "12342346",
        "12342347",
        "12342348",   
        "12342349"
    ],

When I use strapi and a component in a collection, I get this:

"playOrder":[
{"id":1,"playOrderItem":"12341234"},
{"id":2,"playOrderItem":"12341235"},
{"id":6,"playOrderItem":"12341236"},
{"id":10,"playOrderItem":"12341237"},
{"id":11,"playOrderItem":"12341238"},
{"id":13,"playOrderItem":"12341239"}
],

Is there any way to create a simple list of strings/numbers like that first example (without using the JSON collection type, I want the people who need to enter this data to not have to know JSON formatting)? I could re-adjust all of my code to parse that new format, but I prefer the cleaner/less data intensive way that it looks currently.

EDIT: This is in V4

You can write your own controller code and get the response in any format which suits for your project. and that’s the only way i think :slight_smile:

1 Like

I see not really a problem in the current solution.

Another solution could be:
You could create a entity strings with a addString-string-Attribute and a collectionOfStrings-text-Attribute.
With beforeCreate- & beforeUpdate-Lifecycle Methods you add the value of addString to collectionOfStrings and clear addString after that.

1 Like

I have the same question. I feel that strapi support is not very good.

There are a few levels of “support”

You can get a paid version of the Enterprise License with dedicated Support.

Or you can use the community support but you are not guaranteed a answer on this.

Also I would customize the controller and do the work there to send it back

Once i know you’re online, can you help me with how to upload assets to azure? i’n tried many examples and they are not working

Would suggest create a topic about it and anyone can help.

I have done that but no response so far

before storing do JSON. stringify, when you get a response then you can use JSON parser to get it in the correct format. JSON was introduced in es6. you can explore more

You can use the plugin strapi-plugin-transformer

then edit or create the file config/plugins.js :

module.exports = ({ env }) => ({
  // ..
  'transformer': {
    enabled: true,
    config: {
      responseTransforms: {
        removeAttributesKey: true,
        removeDataKey: true,
      },
      hooks: {
        postResponseTransform : (ctx) => {
          ctx.body.data = ctx.body.data.map((playlist) => ({
            ...playlist,
            playOrder: playlist.playOrder.map((item) => item.playOrderItem)
          }));
        },
      },
    }
  },
  // ..
});

that’s the JSON I get at http://localhost:1337/api/playlists?populate=%2A :

{
   "data":[
      {
         "id":1,
         "name":"playlist #1",
         "createdAt":"2024-01-04T14:54:28.710Z",
         "updatedAt":"2024-01-04T14:54:28.710Z",
         "playOrder":[
            "12342345",
            "12342346",
            "12342347",
            "12342348",
            "12342349"
         ]
      }
   ],
   "meta":{
      "pagination":{
         "page":1,
         "pageSize":25,
         "pageCount":1,
         "total":1
      }
   }
}