forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonl.rs
149 lines (120 loc) · 4.2 KB
/
jsonl.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use serde_json::Value;
use std::fs;
use std::io::{self, BufRead, BufReader};
use crate::config::Config;
use crate::util;
use crate::CliResult;
use serde::Deserialize;
static USAGE: &str = "
Converts a newline-delimited JSON file (.ndjson or .jsonl, typically) into
a CSV file.
The command tries to do its best but since it is not possible to
straightforwardly convert JSON lines to CSV, the process might lose some complex
fields from the input.
Also, it will fail if the JSON documents are not consistent with one another,
as the first JSON line will be use to infer the headers of the CSV output.
Usage:
qsv jsonl [options] [<input>]
qsv jsonl --help
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
";
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_output: Option<String>,
}
fn recurse_to_infer_headers(value: &Value, headers: &mut Vec<Vec<String>>, path: Vec<String>) {
match value {
Value::Object(map) => {
for (key, value) in map.iter() {
match value {
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
let mut full_path = path.clone();
full_path.push(key.to_string());
headers.push(full_path);
}
Value::Object(_) => {
let mut new_path = path.clone();
new_path.push(key.to_string());
recurse_to_infer_headers(value, headers, new_path);
}
_ => {}
}
}
}
_ => {
headers.push(vec![String::from("value")]);
}
}
}
fn infer_headers(value: &Value) -> Option<Vec<Vec<String>>> {
let mut headers: Vec<Vec<String>> = Vec::new();
recurse_to_infer_headers(value, &mut headers, Vec::new());
Some(headers)
}
fn get_value_at_path(value: &Value, path: &Vec<String>) -> Option<Value> {
let mut current = value;
for key in path.iter() {
match current.get(key) {
Some(new_value) => {
current = new_value;
}
None => {
return None;
}
}
}
Some(current.to_owned())
}
fn json_line_to_csv_record(value: &Value, headers: &Vec<Vec<String>>) -> csv::StringRecord {
let mut record = csv::StringRecord::new();
for path in headers {
let value = get_value_at_path(value, path);
if let Some(value) = value {
record.push_field(&match value {
Value::Null => String::new(),
Value::Bool(v) => {
if v {
String::from("true")
} else {
String::from("false")
}
}
Value::Number(v) => v.to_string(),
Value::String(v) => v,
_ => String::new(),
});
} else {
record.push_field("");
}
}
record
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let rdr: Box<dyn BufRead> = match args.arg_input {
None => Box::new(BufReader::new(io::stdin())),
Some(p) => Box::new(BufReader::new(fs::File::open(p)?)),
};
let mut headers: Vec<Vec<String>> = Vec::new();
let mut headers_emitted: bool = false;
for line in rdr.lines() {
let value: Value = serde_json::from_str(&line?).expect("Could not parse line as JSON!");
if !headers_emitted {
if let Some(h) = infer_headers(&value) {
headers = h;
let headers_formatted =
headers.iter().map(|v| v.join(".")).collect::<Vec<String>>();
let headers_record = csv::StringRecord::from(headers_formatted);
wtr.write_record(&headers_record)?;
}
headers_emitted = true;
}
let record = json_line_to_csv_record(&value, &headers);
wtr.write_record(&record)?;
}
Ok(())
}