An open API service indexing awesome lists of open source software.

https://github.com/fabervitale/deno-ndjson

Read, write, parse and serialize new line delimited JSON in deno: http://ndjson.org/
https://github.com/fabervitale/deno-ndjson

deno ndjson parse typescript

Last synced: 25 days ago
JSON representation

Read, write, parse and serialize new line delimited JSON in deno: http://ndjson.org/

Awesome Lists containing this project

README

        

# deno-ndjson

## Decription

Read, write, parse and serialize newline delimited json, or
[ndjson](http://ndjson.org/) for short.

## Usage

### parseNdjson

Parses the content of a
[Deno.Reader](https://doc.deno.land/https/github.com/denoland/deno/releases/latest/download/lib.deno.d.ts#Deno.Reader).

Ignores parsing errors if options.strict is `false`.

```typescript
async function* parseNdjson(
reader: Deno.Reader,
options?: { strict: boolean },
): AsyncIterableIterator;
```

#### example

```typescript
import { parseNdjson } from "https://deno.land/x/[email protected]/mod.ts";

let file: Deno.File | null = null;

try {
file = await Deno.open("");

for await (const parsed of parseNdjson(file)) {
console.log(parsed);
}
} catch (readError) {
// handle error
} finally {
file?.close();
}
```

[source](./lib/parse.ts)

### readNdjson

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

```typescript
async function readNdjson(filePath: string): Promise;
```

#### example

```typescript
import { readNdjson } from "https://deno.land/x/[email protected]/mod.ts";

const parsed = await readNdjson("");
```

[source](./lib/read.ts)

### serializeNdJson

Serializes the content of an array.

```typescript
function serializeNdJson(data: unknown[]): string;
```

#### example

```typescript
import { serializeNdJson } from "https://deno.land/x/[email protected]/mod.ts";

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

[source](./lib/serialize.ts)

### writeNdjson

Writes the content of an array to a file in ndjson format.

Optional third argument is
[Deno.WriteFileOptions](https://doc.deno.land/https/github.com/denoland/deno/releases/latest/download/lib.deno.d.ts#Deno.WriteFileOptions)
and is passed down to the writer.

```typescript
async function writeNdjson(
filePath: string,
data: unknown[],
options?: Deno.WriteFileOptions,
): Promise;
```

#### example

```typescript
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("", toBeWritten, { append: true });
```

[source](./lib/write.ts)

---

## License

[MIT](./LICENSE)