forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonl.rs
178 lines (147 loc) · 5.13 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
static USAGE: &str = "
Convert newline-delimited JSON (JSONL/NDJSON) to CSV.
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
jsonl options:
--ignore-errors Skip malformed input lines.
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
";
use std::{
fs,
io::{self, BufRead, BufReader},
};
use serde::Deserialize;
use serde_json::Value;
use crate::{config::Config, util, CliResult};
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_output: Option<String>,
flag_ignore_errors: bool,
}
#[allow(clippy::needless_pass_by_value)]
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(_)
| Value::Array(_) => {
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);
}
#[allow(unreachable_patterns)]
_ => {}
}
}
}
_ => {
headers.push(vec![String::from("value")]);
}
}
}
#[allow(clippy::unnecessary_wraps)]
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: &[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.clone())
}
fn json_line_to_csv_record(value: &Value, headers: &[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::Bool(v) => {
if v {
String::from("true")
} else {
String::from("false")
}
}
Value::Number(v) => v.to_string(),
Value::String(v) => v,
Value::Array(v) => v
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join(","),
_ => 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 (rowidx, line) in rdr.lines().enumerate() {
let value: Value = match serde_json::from_str(&line?) {
Ok(v) => v,
Err(e) => {
if args.flag_ignore_errors {
continue;
} else {
let human_idx = rowidx + 1; // not zero based
return fail_format!(
r#"Could not parse line {human_idx} as JSON! - {e}
Use `--ignore-errors` option to skip malformed input lines.
Use `tojsonl` command to convert _to_ jsonl instead of _from_ jsonl."#,
);
}
}
};
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(())
}