Created
April 13, 2024 18:00
-
-
Save catdevnull/431944dbb8f174e487f5b2cfdc1bda0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { 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