Matthew Collins
Matthew Collins

Reputation: 31

What is causing a double comma in my csv output when using `",\n`?

I am writing a script that is designed to take in an array and replace a designated row in a csv(buffer) then output a csv(buffer) in nodejs. However, I have found that whenever I have the following combination of characters: ",\n", it is doubling the comma. I have tried using \r instead or \n, but the system I am importing the csv has issues with the \r. I also found that by adding an extra whitespace: ", \n it prevents the double comma, but again, the system I'm importing the final result into won't accept the extra space. Does anyone know what is causing the extra comma and/or a way to not get the extra comma?

Script that replaces CSV row:

node.on('input', function(msg) {
    node.rowNumber = msg.rowNumber || config.rowNumber || 0; //Row being replaced.
    node.newRow = msg.newRow || config.newRow; //New Row Array or Comma Separated String
    var payload = msg.file || config.file || RED.util.getMessageProperty(msg, "payload"); //File path or buffer.

    if (!Buffer.isBuffer(payload)) {
        payload = payload.replace('\\', '/');
        payload = fs.readFileSync(payload);
    }

    if (!Array.isArray(this.newRow)) {
        node.newRow = node.newRow.split(',');
    }

    var dataArray = [];
    var csvArr = [];

    const readable = new Stream.Readable()
    readable._read = () => {}
    readable.push(payload)
    readable.push(null)

    readable.pipe(csv())
      .on('data', function (data) {
        dataArray.push(data);
      })
      .on('end', function(){
          csvArr.push(Object.keys(dataArray[0]));
          dataArray.forEach((item, i) => {
              csvArr.push(_.values(item));
          });
          if (node.rowNumber == 0) {
              csvArr.push(node.newRow);
          }
          else {
              csvArr.splice(node.rowNumber - 1, 1, node.newRow);
          }
          var finalCSV = csvArr.join('\n');
          msg.payload = Buffer.from(finalCSV);
          node.send(msg); //Returns the msg object
      });
  });

Input:

[
    `""{
        """"actions"""":{
            """"validation"""":[
            
            ],
            """"reconciliation"""":[
            
            ]
        },
        """"enforce_all_required_fields"""":"""""""",
        """"form_history"""":""""12c2acda35980131f98acf2a39c1aafe"""",
        """"form_id"""":""""228"""",
        """"options"""":[
        
        ],
        """"record_action"""":""""both"""",
        """"secondary_form_history"""":"""""""",
        """"secondary_form_id"""":""""0"""",
        """"secondary_form_name"""":"""""""",
        """"secondary_is_tier1_form"""":"""""""",
        """"selected_columns"""":[
            """"field_9326"""",
            """"field_3742_first"""",
            """"field_3742_last"""",
            """"field_9325"""",
            """"field_9327"""",
        ],
        """"skip_headers"""":"""""""",
        """"target_match_type"""":""""""""
    }""`
]

Undesired output:

"{
    ""actions"":{
        ""validation"":[

        ],
        ""reconciliation"":[

        ]
    },
    ""enforce_all_required_fields"":"""",,
    ""form_history"":""12c2acda35980131f98acf2a39c1aafe"",,
    ""form_id"":""228"",,
    ""options"":[

    ],
    ""record_action"":""both"",,
    ""secondary_form_history"":"""",,
    ""secondary_form_id"":""0"",,
    ""secondary_form_name"":"""",,
    ""secondary_is_tier1_form"":"""",,
    ""selected_columns"":[
        ""field_9326"",,
        ""field_3742_first"",,
        ""field_3742_last"",,
        ""field_9325"",,
        ""field_9327"",,
    ],
    ""skip_headers"":"""",,
    ""target_match_type"":""""
}"

Notice the double commas?

Upvotes: 1

Views: 273

Answers (0)

Related Questions