Read, write, parse and serialize newline delimited json, or ndjson for short.
Parses the content of a Deno.Reader.
Ignores parsing errors if options.strict is false
.
async function* parseNdjson<T extends JSONData>(
reader: Deno.Reader,
options?: { strict: boolean },
): AsyncIterableIterator<T>;
import { parseNdjson } from "https://deno.land/x/[email protected]/mod.ts";
let file: Deno.File | null = null;
try {
file = await Deno.open("<filepath_here>");
for await (const parsed of parseNdjson(file)) {
console.log(parsed);
}
} catch (readError) {
// handle error
} finally {
file?.close();
}
Reads a Ndjson file and returns an array of parsed lines.
async function readNdjson<T extends JSONData[]>(filePath: string): Promise<T>;
import { readNdjson } from "https://deno.land/x/[email protected]/mod.ts";
const parsed = await readNdjson("<file_path_here>");
Serializes the content of an array.
function serializeNdJson(data: unknown[]): string;
import { serializeNdJson } from "https://deno.land/x/[email protected]/mod.ts";
const serialized: string = serializeNdJson([
{ who: "let" },
{ the: "dogs" },
{ out: "!" },
]);
Writes the content of an array to a file in ndjson format.
Optional third argument is Deno.WriteFileOptions and is passed down to the writer.
async function writeNdjson(
filePath: string,
data: unknown[],
options?: Deno.WriteFileOptions,
): Promise<void>;
import { writeNdjson } from "https://deno.land/x/[email protected]/mod.ts";
const toBeWritten = [
{ message: "qui", level: "info", timestamp: "2020-05-08T14:05:25.091Z" },
{ message: "que", level: "info", timestamp: "2020-05-08T14:05:25.096Z" },
{ message: "quod", level: "info", timestamp: "2020-05-08T14:05:25.104Z" },
];
await writeNdjson("<file_path_here>", toBeWritten, { append: true });