Search code examples
javascriptnode.jscsvpuppeteerwebautomation

How do I grab data from a csv/excel file into my code?


Im using puppeteer to automate doing a form, I want to directly grab data from a csv file and input into the form. How do I go about doing that? The CSV file is : FirstName,Number,Address,Country,meliz,123,High Road,US.

Form Link: https://forms.gle/UUQUgTm9tpmrGSnF8

Update: Below is the code I used to convert to JSON

const csvToJson = require('convert-csv-to-json')

const json = csvToJson.getJsonFromCsv("Book1.csv");

for(let i=0; i<json.length;i++){
  console.log(json[i]);
 }

This is the result

{   FirstName: 'meliz',   Number: '123',   Address: 'High Road',   Country: 'US' }

I want to get a specific value e.g FirstName

How would I do it?


Solution

  • Try converting the CSV file into JSON and then flow that object into your code.

    const csvToJson = require('convert-csv-to-json');
    
    const fileInputName = 'myInputFileName.csv';
    
    const json = csvToJson.getJsonFromCsv("myInputFile.csv"); // you can use this json into your code.
    
    // looping on the json
    for (let i = 0; i < json.length; i++) {
      console.log(json[i]);
      console.log(json[i].FirstName);
      console.log(json[i].Number);
      console.log(json[i].Address);
      console.log(json[i].Country);
    }
    

    csv-to-json this package will help you in converting your csv to json.