Created
December 29, 2021 14:58
-
-
Save squamuglia/b99798f38f910b8bd648b97c0fa068b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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