Read, write, parse and serialize new line delimited JSON in deno

deno-ndjson

Decription

Read, write, parse and serialize newline delimited json, or ndjson for short.

Usage

parseNdjson

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>
example
import { parseNdjson } from 'https://deno.land/x/ndjson@v1.0.4/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();
}

source

readNdjson

Reads a Ndjson file and returns an array of parsed lines.

async function readNdjson<T extends JSONData[]>( filePath: string): Promise<T> 
example
import { readNdjson } from 'https://deno.land/x/ndjson@v1.0.4/mod.ts';

const parsed = await readNdjson("<file_path_here>");

source

serializeNdJson

Serializes the content of an array.

function serializeNdJson(data: unknown[]): string
example
import { serializeNdJson } from 'https://deno.land/x/ndjson@v1.0.4/mod.ts';

const serialized: string = serializeNdJson([
  { who: "let" },
  { the: "dogs" },
  { out: "!" },
]);

source

writeNdjson

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>
example
import { writeNdjson } from 'https://deno.land/x/ndjson@v1.0.4/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 }); 

source

Download Details:

Author: FaberVitale

Demo: http://ndjson.org/

Source Code: https://github.com/FaberVitale/deno-ndjson

#deno #nodejs #node #javascript

Read, write, parse and serialize new line delimited JSON in deno
10.90 GEEK