Skip to content

Instantly share code, notes, and snippets.

@squamuglia
Created December 29, 2021 14:58
Show Gist options
  • Save squamuglia/b99798f38f910b8bd648b97c0fa068b8 to your computer and use it in GitHub Desktop.
Save squamuglia/b99798f38f910b8bd648b97c0fa068b8 to your computer and use it in GitHub Desktop.
import fs from "fs";
const convertNDJSONtoJSON =
(ndjsonPath: string, jsonOutputPath: string) => async () => {
const data = await new Promise((res, rej) => {
const dataNDJSON: any[] = [];
const stream = fs.createReadStream(ndjsonPath).pipe(ndjson.parse());
stream.on("data", (obj: any) => dataNDJSON.push(obj));
stream.on("end", () => res(dataNDJSON));
stream.on("error", rej);
});
await writeFile(jsonOutputPath, data);
};
const convertJSONtoNDJSON =
(jsonPath: string, ndjsonOutputPath: string) => async () => {
const json_raw = await readFileToString(jsonPath);
const jsonData = json_raw && JSON.parse(json_raw);
const stream = ndjson.stringify();
stream.on("data", (line) => {
fs.appendFile(ndjsonOutputPath, line, (err) => err && console.error(err));
});
jsonData.forEach((obj: any) => stream.write(obj));
stream.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment