Skip to content

Instantly share code, notes, and snippets.

@catdevnull
Created April 13, 2024 18:00
Show Gist options
  • Save catdevnull/431944dbb8f174e487f5b2cfdc1bda0f to your computer and use it in GitHub Desktop.
Save catdevnull/431944dbb8f174e487f5b2cfdc1bda0f to your computer and use it in GitHub Desktop.
import { readFile } from "node:fs/promises";
const jsonl = await readFile("/dev/stdin", "utf-8");
const lines = jsonl.split("\n").filter((s) => !!s.trim());
const objects = lines.map((l) => JSON.parse(l));
let keys = [];
for (const object of objects) {
for (const key of Object.keys(object)) {
if (!keys.includes(key)) keys.push(key);
}
}
console.log(keys.join(","));
for (const object of objects) {
console.log(
keys
.map(
(key) =>
`"${("" + object[key]).replaceAll('"', '""').replaceAll("\n", "\\n")}"`
)
.join(",")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment