How to import data from a json file? I am trying to import 10k articles

System Information
  • Strapi Version: 3.5.1
  • Operating System: Ubuntu 20.04
  • Database: MongoDB Atlas
  • Node Version: 14.15.5
  • NPM Version: 6.14.11
  • Yarn Version: 1.22.5

I need to import 10k articles to a content type, I already tried this plugin and It didn’t work, says please wait and then does not import anything. I also tried following this video and it throws this error: error TypeError: Cannot read property '_text' of undefined.

I would appreaciate any help Its been two days trying to import a simple json file,
the json file is structured as an object thats why I have to pass it like that on the forEach otherwise I will get an error saying that is not a function.

PD: If It helps, I’m getting the json file from a postgress db with prisma.

PD2: removing the _text part, removes the error but creates an empty entry.

this is the code I am using on config/functions/bootstrap.js:
"use strict";

/**
 * An asynchronous bootstrap function that runs before
 * your application gets started.
 *
 * This gives you an opportunity to set up your data model,
 * run jobs, or perform some special logic.
 *
 * See more details here: https://strapi.io/documentation/developer-docs/latest/concepts/configurations.html#bootstrap
 */

module.exports = () => {
  const posts = require("fs").readFileSync("./data/allposts.json", "utf8");

  const data = JSON.parse(posts);

  Object.keys(data).forEach((entry) => {
    strapi.services.post.create({
      title: entry.title._text,
    });
  });
};

I’m do this job with MySQL 8
MySQL 8 have a huge set of JSON functions.

  1. I’m create table with one JSON field and put my JSON inside it (about 3000 records)
  2. Next I’m put each record to another table with id + JSON and so on

Could you please elaborate on how you do that? I find this for creating a JSON field but I dont understand too much of it. I have the json file and the mysql table of the data and I know how to import an existing table but don’t know how to create a content-type based on it.

I was hoping to import the json file, but If I can just import the table and create a content type that would be better.

Here you are doing foreach for the Keys(Keys, if it is an Object, and Index if it is an array). So the entry, in this case, is not an object.

That’s the correct approach

Object.keys(data).forEach((entry) => {
    strapi.services.post.create({
      title: data[entry].title._text,
    });
  });

I would recommend studying a little bit about Object and his prototypes. That would help to avoid a future error like this one.

I will try that, thanks. I am a newbie so that’s understandable, thank you for pointing out the error I appreciate that.

1 Like

I’m do it many times ago and don’t understand details.

But If you need help - you can send JSON file to me - and I’m import it to MySQL 8 and write sql sequence for you (it depends on input file)