0

I am trying to return a row value on a matching string.

I begin with a csv that doesn't have header details. After loading the csv into memory, I think I need to first add headers and then convert the data to json, then loop through the data to find the correct object in the array.

I have used highland to create a read stream and output to objects. However, the line break characters \r and \r\n are not getting parsed out and are injected into the value strings.

This approach also doesn't seem to parse the entire file, it is outputting 3 lines.

highland(fs.createReadStream('example.csv', 'utf8'))
  .map(line => line.split(','))
  .map(parts => ({
    a: parts[0],
    b: parts[1],
    c: parts[2]
}))
.each(x => console.log(x))

It would be nice to structure the CSV as JSON and then use the .filter() method to match the record.

Actual output of 1000+ line file

{ a: '', b: 'CD53110' }
{ a: '\nRD40115', b: 'CD40315' }
{ a: '', b: '63\r\nRE15468' }
{ a: '96798', b: '5\r\nRR60899' }

Example input snippet

,CD510,13
,T9069,65
RCM22,TC633,101
RC023,87693,16
M2024,T7636,109

Note: first few rows only contain 'b' and 'c' columns.

7
  • Convert the CSV into an array/object, adding the headers after is trivially easy. Commented Jun 28, 2018 at 14:01
  • What method would you use? Commented Jun 28, 2018 at 14:08
  • Loop through the created array/object, then create another array/object in the same format with updated key names. Commented Jun 28, 2018 at 14:10
  • Could you please provide an example of what you mean? Commented Jun 28, 2018 at 14:13
  • @Matthew What have you tried? stackoverflow.com/help/on-topic 3. Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Commented Jun 28, 2018 at 14:28

1 Answer 1

0

You can write the headers out as a string, set the csv to memory, and concatenate the two.

Then with the csvtojson module you can map the csv to json and use the native filter function to return the matching row.

const fs = require('fs')
const csv = require('csvtojson')

let headers = 'a,b,c\n'
let file = fs.readFileSync(__dirname + 'example.csv', 'utf8')
let strToMatch = 'def123'

let c = headers + file

csv().fromString(c)
  .then((json) => {
    let arr = json.filter(el => el.a == strToMatch)
    res.json({
      a: arr[0]['a'],
      b: arr[0]['b'],
      c: arr[0]['c']
    })
})
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.