Can import data by JSON or CSV?

Hello it’s possible importa data to strapi through JSON or CSV file?

To upload them in Strapi UI, you should develop a custom plugin for this.

By using API, it’s easier since you don’t need a frontend inside the strapi UI.
You should write a custom controller that will process your JSON/CSV and upload data using core services.

So let’s take a look at a custom controller for this. I’ll provide a proof of concept, not the complete working code.

./api/articles/controllers/upload.js:

// You should find some modules that convert CSV to Array of objects. 
// For json you can use just JSON.parse() if fields are identical of both cases. 
const {importLibraryThatParsesCSV}  = require('{importLibraryThatParsesCSV}');

module.exports = {
  /**
   * Get JSON or CSV file and create entries in strapi.
   */
 async uploadFile(ctx) {
    const { request: { body, files: { files } = {} } } = ctx; //getting the file.
    let fileTransformedToArray; //preparing empty array for storing entities.

    // we will use different parse methods for each file type
    if (files.type == 'text/csv') {
      fileTransformedToArray = importLibraryThatParsesCSV(files);
    }
    if (files.type == 'application/json') {
      fileTransformedToArray = importLibraryThatParsesJSON(files)  // or JSON.parse()
    }
    // now, as we have all data parsed as an Array, 
    // we can apply a foreach on it and create entities one by one
    fileTransformedToArray.forEach(entity=> {
      await strapi.services.articles.create(entity);
    });
  },
};
2 Likes

thanks but looks be a PRO job! i am a low level developer

can’t vouch for this plugin but its available from the awesome strapi github repository.

pouyamiralayi/strapi-import-content-plugin-tutorial: strapi plugin for importing csv & rss (github.com)

1 Like

see this example (I migrated from GitHub discussions): ValidationError on POST request with csv file in body #7574

2 Likes

Thanks, it looks interesting, have you tried it, does it work?

I built the solution :stuck_out_tongue: So I would hope so. (I’m derrickmehaffy :laughing:)

4 Likes

good to know that someone that works for strapi built it, i’ll try it

haven’t tried it yet but i will because its the solution i was definetly looking for

Hey is the plugin working for importing csv to strapi

1 Like