-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2tsv.ts
More file actions
executable file
·47 lines (37 loc) · 931 Bytes
/
json2tsv.ts
File metadata and controls
executable file
·47 lines (37 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env -S deno run --allow-read
import { parseArgs } from "@std/cli";
import { TextLineStream } from "@std/streams";
const USAGE = `Convert JSON to TSV format.
Input can be either a file or stdin
Usage:
json2tsv [--input <file>]
Example:
cat file.json | json2tsv
json2tsv --input data.json
`;
const args = parseArgs(Deno.args, {
alias: {
h: "help",
i: "input",
},
});
if (args.help) {
console.error(USAGE);
Deno.exit(0);
}
const input = args.input ? await Deno.open(args.input) : Deno.stdin;
const lineStream = input.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
for await (const line of lineStream) {
try {
const obj = JSON.parse(line);
const tsv: string[] = [];
Object.values(obj).forEach((value) => {
tsv.push(JSON.stringify(value));
});
console.log(tsv.join("\t"));
} catch (error) {
console.error(error);
}
}