0

I'm creating a MySQL database and I want to insert about 1000 records that are structured in a JSON array

This is the JSON array that I'm importing:

 [
  {
   "DESCRIPTION": "انفرتر 1   حصان أحادي",
   "TYPE": "ODE-2-12075-1K012XX",
   "CLASSIFICATION": "VFD",
   "BRAND": "INVERTEK",
   "ORIGIN": "UK",
   "FAMILY": "ODE-2",
   "ar_classification": "انفرتر"
  },
 {
  "DESCRIPTION": "انفرتر 2   حصان أحادي",
  "TYPE": "ODE-2-120150-1K012XX",
  "CLASSIFICATION": "VFD",
  "BRAND": "INVERTEK",
  "ORIGIN": "UK",
  "FAMILY": "ODE-2",
  "ar_classification": "انفرتر"
 },
 {
  "DESCRIPTION": "انفرتر كتيم 3 حصان أحادي بدون\nمقاومة ومفتاح",
  "TYPE": "ODE-2-24400-3K04N-XX",
  "CLASSIFICATION": "VFD IP55",
  "BRAND": "INVERTEK",
  "ORIGIN": "UK",
  "FAMILY": "ODE-2",
  "ar_classification": "انفرتر"
 }
]
  const {readFileSync} = require('fs')
  //Here is where the Json array is located
  const first = readFileSync('./Zahabi-Products.json', 'utf8')
 const products = JSON.parse(first)

   
    pool.query("INSERT INTO products SET ?", products, 
        function(err, result) {
        if(err) throw err;
        console.log('data inserted');
     });

But the result is that only the first object in the array is being inserted

How could I insert all of the array

1 Answer 1

1

I tried this and it worked, but I think that is not the perfect answer

var test02 = {}
for(var i = 0; i <= products.length; i++ ){
   test02 = products[i]
   pool.query("INSERT INTO products SET ?",test02 , 
   function(err, result) {
   if(err) throw err;
   console.log('data inserted');
});

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.