0

I'm trying to convert a TSV file into JSON and write it to disk using text2json.

Input data

There is an empty line at the end

U+2B695 shī
U+2B699 pū
U+2B6DB zhī
U+2B6DE jué
U+2B6E2 níng
U+2B6F6 chì
U+2B6F8 tí

Test

I've this test running with ava

import fs from "fs";
import test from "ava";
import jsonfile from "jsonfile";
import dataminer from "../src/dataminer";

test("extractPronunciation()", t => {
  const filepath = "src/codepoint-ruby.tsv";

  const data = dataminer.convertToJson(filepath, { separator: " " });

  t.is(data > 0);
});

Code

And this method based on text2json:

import jsonfile from "jsonfile";
import text2json from "text2json";

export default {
  convertToJson: (filepath, options) => {
    const data = [];
    const parse = new text2json.Parser({ ...options, hasHeader: true });

    parse
      .text2json(filepath)
      .on("row", row => {
        data.push(row);
      })
      .on("end", () => {
        console.log("Done >>>>>");
        return data;
      });
  },
};

Question

I see not trace of the end event being triggered, and the convertToJson() return nothing so my test fail, am I missing something?

Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178

1 Answers1

2

In your approach, you're filling the data array by reading asynchronously from a stream, instead of putting the whole file in memory and doing it synchronously. (And that's why you've got to use the on event to push data to your array).

This means that, in the same way you use

parse.text2json(filepath).on("row", row => {
      data.push(row);
});

you also need to use an end event to log the final result

parse.text2json(filepath)
    .on("row", row => {
        data.push(row);
    })
    .on('end', () => {
        console.log(data)
    });
ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • the `end` event doesn't seem to be triggered in my case. I updated the question to provide more context – Édouard Lopez Mar 27 '17 at 21:59
  • Yeah I saw you filed an issue. However, even if you tried the callback approach you wouldn't get a synchronous response. (you'd have to use the callback, of course). Now, regarding the "end" event not happening, you could -again- try the callback approach, or perhaps listen to the `error` event, as [the only exit point](https://github.com/nilobarp/text2json/blob/master/src/index.ts#L156) after reading the file must either callback or emit end. – ffflabs Mar 27 '17 at 23:09