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

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

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

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/[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();
}

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/[email protected]/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/[email protected]/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/[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 }); 

source

Download Details:

Author: FaberVitale

Demo: http://ndjson.org/

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

deno nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings