source: node_modules/d3-dsv/README.md@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 25.8 KB
Line 
1# d3-dsv
2
3This module provides a parser and formatter for delimiter-separated values, most commonly [comma-](https://en.wikipedia.org/wiki/Comma-separated_values) (CSV) or tab-separated values (TSV). These tabular formats are popular with spreadsheet programs such as Microsoft Excel, and are often more space-efficient than JSON. This implementation is based on [RFC 4180](http://tools.ietf.org/html/rfc4180).
4
5Comma (CSV) and tab (TSV) delimiters are built-in. For example, to parse:
6
7```js
8d3.csvParse("foo,bar\n1,2"); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
9d3.tsvParse("foo\tbar\n1\t2"); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
10```
11
12Or to format:
13
14```js
15d3.csvFormat([{foo: "1", bar: "2"}]); // "foo,bar\n1,2"
16d3.tsvFormat([{foo: "1", bar: "2"}]); // "foo\tbar\n1\t2"
17```
18
19To use a different delimiter, such as “|” for pipe-separated values, use [d3.dsvFormat](#dsvFormat):
20
21```js
22const psv = d3.dsvFormat("|");
23
24console.log(psv.parse("foo|bar\n1|2")); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
25```
26
27For easy loading of DSV files in a browser, see [d3-fetch](https://github.com/d3/d3-fetch)’s [d3.csv](https://github.com/d3/d3-fetch/blob/master/README.md#csv), [d3.tsv](https://github.com/d3/d3-fetch/blob/master/README.md#tsv) and [d3.dsv](https://github.com/d3/d3-fetch/blob/master/README.md#dsv) methods.
28
29## Installing
30
31If you use npm, `npm install d3-dsv`. You can also download the [latest release on GitHub](https://github.com/d3/d3-dsv/releases/latest). For vanilla HTML in modern browsers, import d3-dsv from Skypack:
32
33```html
34<script type="module">
35
36import {csvParse} from "https://cdn.skypack.dev/d3-dsv@3";
37
38const data = csvParse(string);
39
40</script>
41```
42
43For legacy environments, you can load d3-dsv’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
44
45```html
46<script src="https://cdn.jsdelivr.net/npm/d3-dsv@3"></script>
47<script>
48
49const data = d3.csvParse(string);
50
51</script>
52```
53
54## API Reference
55
56<a name="csvParse" href="#csvParse">#</a> d3.<b>csvParse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
57
58Equivalent to [dsvFormat](#dsvFormat)(",").[parse](#dsv_parse). Note: requires unsafe-eval [content security policy](#content-security-policy).
59
60<a name="csvParseRows" href="#csvParseRows">#</a> d3.<b>csvParseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
61
62Equivalent to [dsvFormat](#dsvFormat)(",").[parseRows](#dsv_parseRows).
63
64<a name="csvFormat" href="#csvFormat">#</a> d3.<b>csvFormat</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
65
66Equivalent to [dsvFormat](#dsvFormat)(",").[format](#dsv_format).
67
68<a name="csvFormatBody" href="#csvFormatBody">#</a> d3.<b>csvFormatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
69
70Equivalent to [dsvFormat](#dsvFormat)(",").[formatBody](#dsv_formatBody).
71
72<a name="csvFormatRows" href="#csvFormatRows">#</a> d3.<b>csvFormatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
73
74Equivalent to [dsvFormat](#dsvFormat)(",").[formatRows](#dsv_formatRows).
75
76<a name="csvFormatRow" href="#csvFormatRow">#</a> d3.<b>csvFormatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
77
78Equivalent to [dsvFormat](#dsvFormat)(",").[formatRow](#dsv_formatRow).
79
80<a name="csvFormatValue" href="#csvFormatValue">#</a> d3.<b>csvFormatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
81
82Equivalent to [dsvFormat](#dsvFormat)(",").[formatValue](#dsv_formatValue).
83
84<a name="tsvParse" href="#tsvParse">#</a> d3.<b>tsvParse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
85
86Equivalent to [dsvFormat](#dsvFormat)("\t").[parse](#dsv_parse). Note: requires unsafe-eval [content security policy](#content-security-policy).
87
88<a name="tsvParseRows" href="#tsvParseRows">#</a> d3.<b>tsvParseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
89
90Equivalent to [dsvFormat](#dsvFormat)("\t").[parseRows](#dsv_parseRows).
91
92<a name="tsvFormat" href="#tsvFormat">#</a> d3.<b>tsvFormat</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
93
94Equivalent to [dsvFormat](#dsvFormat)("\t").[format](#dsv_format).
95
96<a name="tsvFormatBody" href="#tsvFormatBody">#</a> d3.<b>tsvFormatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
97
98Equivalent to [dsvFormat](#dsvFormat)("\t").[formatBody](#dsv_formatBody).
99
100<a name="tsvFormatRows" href="#tsvFormatRows">#</a> d3.<b>tsvFormatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
101
102Equivalent to [dsvFormat](#dsvFormat)("\t").[formatRows](#dsv_formatRows).
103
104<a name="tsvFormatRow" href="#tsvFormatRow">#</a> d3.<b>tsvFormatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
105
106Equivalent to [dsvFormat](#dsvFormat)("\t").[formatRow](#dsv_formatRow).
107
108<a name="tsvFormatValue" href="#tsvFormatValue">#</a> d3.<b>tsvFormatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
109
110Equivalent to [dsvFormat](#dsvFormat)("\t").[formatValue](#dsv_formatValue).
111
112<a name="dsvFormat" href="#dsvFormat">#</a> d3.<b>dsvFormat</b>(<i>delimiter</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js)
113
114Constructs a new DSV parser and formatter for the specified *delimiter*. The *delimiter* must be a single character (*i.e.*, a single 16-bit code unit); so, ASCII delimiters are fine, but emoji delimiters are not.
115
116<a name="dsv_parse" href="#dsv_parse">#</a> *dsv*.<b>parse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
117
118Parses the specified *string*, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
119
120Unlike [*dsv*.parseRows](#dsv_parseRows), this method requires that the first line of the DSV content contains a delimiter-separated list of column names; these column names become the attributes on the returned objects. For example, consider the following CSV file:
121
122```
123Year,Make,Model,Length
1241997,Ford,E350,2.34
1252000,Mercury,Cougar,2.38
126```
127
128The resulting JavaScript array is:
129
130```js
131[
132 {"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
133 {"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
134]
135```
136
137The returned array also exposes a `columns` property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary). For example:
138
139```js
140data.columns; // ["Year", "Make", "Model", "Length"]
141```
142
143If the column names are not unique, only the last value is returned for each name; to access all values, use [*dsv*.parseRows](#dsv_parseRows) instead (see [example](https://observablehq.com/@d3/parse-csv-with-duplicate-column-names)).
144
145If a *row* conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the `+` operator), but better is to specify a *row* conversion function. See [d3.autoType](#autoType) for a convenient *row* conversion function that infers and coerces common types like numbers and strings.
146
147If a *row* conversion function is specified, the specified function is invoked for each row, being passed an object representing the current row (`d`), the index (`i`) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by *dsv*.parse; otherwise, the returned value defines the corresponding row object. For example:
148
149```js
150const data = d3.csvParse(string, (d) => {
151 return {
152 year: new Date(+d.Year, 0, 1), // lowercase and convert "Year" to Date
153 make: d.Make, // lowercase
154 model: d.Model, // lowercase
155 length: +d.Length // lowercase and convert "Length" to number
156 };
157});
158```
159
160Note: using `+` rather than [parseInt](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt) or [parseFloat](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat) is typically faster, though more restrictive. For example, `"30px"` when coerced using `+` returns `NaN`, while parseInt and parseFloat return `30`.
161
162Note: requires unsafe-eval [content security policy](#content-security-policy).
163
164<a name="dsv_parseRows" href="#dsv_parseRows">#</a> <i>dsv</i>.<b>parseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
165
166Parses the specified *string*, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.
167
168Unlike [*dsv*.parse](#dsv_parse), this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length. For example, consider the following CSV file, which notably lacks a header line:
169
170```
1711997,Ford,E350,2.34
1722000,Mercury,Cougar,2.38
173```
174
175The resulting JavaScript array is:
176
177```js
178[
179 ["1997", "Ford", "E350", "2.34"],
180 ["2000", "Mercury", "Cougar", "2.38"]
181]
182```
183
184If a *row* conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the `+` operator), but better is to specify a *row* conversion function. See [d3.autoType](#autoType) for a convenient *row* conversion function that infers and coerces common types like numbers and strings.
185
186If a *row* conversion function is specified, the specified function is invoked for each row, being passed an array representing the current row (`d`), the index (`i`) starting at zero for the first row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by *dsv*.parse; otherwise, the returned value defines the corresponding row object. For example:
187
188```js
189const data = d3.csvParseRows(string, (d, i) => {
190 return {
191 year: new Date(+d[0], 0, 1), // convert first colum column to Date
192 make: d[1],
193 model: d[2],
194 length: +d[3] // convert fourth column to number
195 };
196});
197```
198
199In effect, *row* is similar to applying a [map](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map) and [filter](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter) operator to the returned rows.
200
201<a name="dsv_format" href="#dsv_format">#</a> <i>dsv</i>.<b>format</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
202
203Formats the specified array of object *rows* as delimiter-separated values, returning a string. This operation is the inverse of [*dsv*.parse](#dsv_parse). Each row will be separated by a newline (`\n`), and each column within each row will be separated by the delimiter (such as a comma, `,`). Values that contain either the delimiter, a double-quote (`"`) or a newline will be escaped using double-quotes.
204
205If *columns* is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in *rows*; the order of columns is nondeterministic. If *columns* is specified, it is an array of strings representing the column names. For example:
206
207```js
208const string = d3.csvFormat(data, ["year", "make", "model", "length"]);
209```
210
211All fields on each row object will be coerced to strings. If the field value is null or undefined, the empty string is used. If the field value is a Date, the [ECMAScript date-time string format](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date-time-string-format) (a subset of ISO 8601) is used: for example, dates at UTC midnight are formatted as `YYYY-MM-DD`. For more control over which and how fields are formatted, first map *rows* to an array of array of string, and then use [*dsv*.formatRows](#dsv_formatRows).
212
213<a name="dsv_formatBody" href="#dsv_formatBody">#</a> <i>dsv</i>.<b>formatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
214
215Equivalent to [*dsv*.format](#dsv_format), but omits the header row. This is useful, for example, when appending rows to an existing file.
216
217<a name="dsv_formatRows" href="#dsv_formatRows">#</a> <i>dsv</i>.<b>formatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
218
219Formats the specified array of array of string *rows* as delimiter-separated values, returning a string. This operation is the reverse of [*dsv*.parseRows](#dsv_parseRows). Each row will be separated by a newline (`\n`), and each column within each row will be separated by the delimiter (such as a comma, `,`). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
220
221To convert an array of objects to an array of arrays while explicitly specifying the columns, use [*array*.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). For example:
222
223```js
224const string = d3.csvFormatRows(data.map((d, i) => {
225 return [
226 d.year.getFullYear(), // Assuming d.year is a Date object.
227 d.make,
228 d.model,
229 d.length
230 ];
231}));
232```
233
234If you like, you can also [*array*.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) this result with an array of column names to generate the first row:
235
236```js
237const string = d3.csvFormatRows([[
238 "year",
239 "make",
240 "model",
241 "length"
242 ]].concat(data.map((d, i) => {
243 return [
244 d.year.getFullYear(), // Assuming d.year is a Date object.
245 d.make,
246 d.model,
247 d.length
248 ];
249})));
250```
251
252<a name="dsv_formatRow" href="#dsv_formatRow">#</a> <i>dsv</i>.<b>formatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
253
254Formats a single array *row* of strings as delimiter-separated values, returning a string. Each column within the row will be separated by the delimiter (such as a comma, `,`). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
255
256<a name="dsv_formatValue" href="#dsv_formatValue">#</a> <i>dsv</i>.<b>formatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
257
258Format a single *value* or string as a delimiter-separated value, returning a string. A value that contains either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
259
260<a name="autoType" href="#autoType">#</a> d3.<b>autoType</b>(<i>object</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/autoType.js "Source")
261
262Given an *object* (or array) representing a parsed row, infers the types of values on the *object* and coerces them accordingly, returning the mutated *object*. This function is intended to be used as a *row* accessor function in conjunction with [*dsv*.parse](#dsv_parse) and [*dsv*.parseRows](#dsv_parseRow). For example, consider the following CSV file:
263
264```
265Year,Make,Model,Length
2661997,Ford,E350,2.34
2672000,Mercury,Cougar,2.38
268```
269
270When used with [d3.csvParse](#csvParse),
271
272```js
273d3.csvParse(string, d3.autoType)
274```
275
276the resulting JavaScript array is:
277
278```js
279[
280 {"Year": 1997, "Make": "Ford", "Model": "E350", "Length": 2.34},
281 {"Year": 2000, "Make": "Mercury", "Model": "Cougar", "Length": 2.38}
282]
283```
284
285Type inference works as follows. For each *value* in the given *object*, the [trimmed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) value is computed; the value is then re-assigned as follows:
286
2871. If empty, then `null`.
2881. If exactly `"true"`, then `true`.
2891. If exactly `"false"`, then `false`.
2901. If exactly `"NaN"`, then `NaN`.
2911. Otherwise, if [coercible to a number](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-tonumber-applied-to-the-string-type), then a number.
2921. Otherwise, if a [date-only or date-time string](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date-time-string-format), then a Date.
2931. Otherwise, a string (the original untrimmed value).
294
295Values with leading zeroes may be coerced to numbers; for example `"08904"` coerces to `8904`. However, extra characters such as commas or units (*e.g.*, `"$1.00"`, `"(123)"`, `"1,234"` or `"32px"`) will prevent number coercion, resulting in a string.
296
297Date strings must be in ECMAScript’s subset of the [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601). When a date-only string such as YYYY-MM-DD is specified, the inferred time is midnight UTC; however, if a date-time string such as YYYY-MM-DDTHH:MM is specified without a time zone, it is assumed to be local time.
298
299Automatic type inference is primarily intended to provide safe, predictable behavior in conjunction with [*dsv*.format](#dsv_format) and [*dsv*.formatRows](#dsv_formatRows) for common JavaScript types. If you need different behavior, you should implement your own row accessor function.
300
301For more, see [the d3.autoType notebook](https://observablehq.com/@d3/d3-autotype).
302
303### Content Security Policy
304
305If a [content security policy](http://www.w3.org/TR/CSP/) is in place, note that [*dsv*.parse](#dsv_parse) requires `unsafe-eval` in the `script-src` directive, due to the (safe) use of dynamic code generation for fast parsing. (See [source](https://github.com/d3/d3-dsv/blob/master/src/dsv.js).) Alternatively, use [*dsv*.parseRows](#dsv_parseRows).
306
307### Byte-Order Marks
308
309DSV files sometimes begin with a [byte order mark (BOM)](https://en.wikipedia.org/wiki/Byte_order_mark); saving a spreadsheet in CSV UTF-8 format from Microsoft Excel, for example, will include a BOM. On the web this is not usually a problem because the [UTF-8 decode algorithm](https://encoding.spec.whatwg.org/#utf-8-decode) specified in the Encoding standard removes the BOM. Node.js, on the other hand, [does not remove the BOM](https://github.com/nodejs/node-v0.x-archive/issues/1918) when decoding UTF-8.
310
311If the BOM is not removed, the first character of the text is a zero-width non-breaking space. So if a CSV file with a BOM is parsed by [d3.csvParse](#csvParse), the first column’s name will begin with a zero-width non-breaking space. This can be hard to spot since this character is usually invisible when printed.
312
313To remove the BOM before parsing, consider using [strip-bom](https://www.npmjs.com/package/strip-bom).
314
315## Command Line Reference
316
317### dsv2dsv
318
319<a name="dsv2dsv" href="#dsv2dsv">#</a> <b>dsv2dsv</b> [<i>options…</i>] [<i>file</i>]
320
321Converts the specified DSV input *file* to DSV (typically with a different delimiter or encoding). If *file* is not specified, defaults to reading from stdin. For example, to convert to CSV to TSV:
322
323```
324csv2tsv < example.csv > example.tsv
325```
326
327To convert windows-1252 CSV to utf-8 CSV:
328
329```
330dsv2dsv --input-encoding windows-1252 < latin1.csv > utf8.csv
331```
332
333<a name="dsv2dsv_help" href="dsv2dsv_help">#</a> dsv2dsv <b>-h</b>
334<br><a href="dsv2dsv_help">#</a> dsv2dsv <b>--help</b>
335
336Output usage information.
337
338<a name="dsv2dsv_version" href="dsv2dsv_version">#</a> dsv2dsv <b>-V</b>
339<br><a href="dsv2dsv_version">#</a> dsv2dsv <b>--version</b>
340
341Output the version number.
342
343<a name="dsv2dsv_out" href="dsv2dsv_out">#</a> dsv2dsv <b>-o</b> <i>file</i>
344<br><a href="dsv2dsv_out">#</a> dsv2dsv <b>--out</b> <i>file</i>
345
346Specify the output file name. Defaults to “-” for stdout.
347
348<a name="dsv2dsv_input_delimiter" href="dsv2dsv_input_delimiter">#</a> dsv2dsv <b>-r</b> <i>delimiter</i>
349<br><a href="dsv2dsv_input_delimiter">#</a> dsv2dsv <b>--input-delimiter</b> <i>delimiter</i>
350
351Specify the input delimiter character. Defaults to “,” for reading CSV. (You can enter a tab on the command line by typing ⌃V.)
352
353<a name="dsv2dsv_input_encoding" href="dsv2dsv_input_encoding">#</a> dsv2dsv <b>--input-encoding</b> <i>encoding</i>
354
355Specify the input character encoding. Defaults to “utf8”.
356
357<a name="dsv2dsv_output_delimiter" href="dsv2dsv_output_delimiter">#</a> dsv2dsv <b>-w</b> <i>delimiter</i>
358<br><a href="dsv2dsv_output_delimiter">#</a> dsv2dsv <b>--output-delimiter</b> <i>delimiter</i>
359
360Specify the output delimiter character. Defaults to “,” for writing CSV. (You can enter a tab on the command line by typing ⌃V.)
361
362<a name="dsv2dsv_output_encoding" href="dsv2dsv_output_encoding">#</a> dsv2dsv <b>--output-encoding</b> <i>encoding</i>
363
364Specify the output character encoding. Defaults to “utf8”.
365
366<a name="csv2tsv" href="#csv2tsv">#</a> <b>csv2tsv</b> [<i>options…</i>] [<i>file</i>]
367
368Equivalent to [dsv2dsv](#dsv2dsv), but the [output delimiter](#dsv2dsv_output_delimiter) defaults to the tab character (\t).
369
370<a name="tsv2csv" href="#tsv2csv">#</a> <b>tsv2csv</b> [<i>options…</i>] [<i>file</i>]
371
372Equivalent to [dsv2dsv](#dsv2dsv), but the [input delimiter](#dsv2dsv_output_delimiter) defaults to the tab character (\t).
373
374### dsv2json
375
376<a name="dsv2json" href="#dsv2json">#</a> <b>dsv2json</b> [<i>options…</i>] [<i>file</i>]
377
378Converts the specified DSV input *file* to JSON. If *file* is not specified, defaults to reading from stdin. For example, to convert to CSV to JSON:
379
380```
381csv2json < example.csv > example.json
382```
383
384Or to convert CSV to a newline-delimited JSON stream:
385
386```
387csv2json -n < example.csv > example.ndjson
388```
389
390<a name="dsv2json_help" href="dsv2json_help">#</a> dsv2json <b>-h</b>
391<br><a href="dsv2json_help">#</a> dsv2json <b>--help</b>
392
393Output usage information.
394
395<a name="dsv2json_version" href="dsv2json_version">#</a> dsv2json <b>-V</b>
396<br><a href="dsv2json_version">#</a> dsv2json <b>--version</b>
397
398Output the version number.
399
400<a name="dsv2json_out" href="dsv2json_out">#</a> dsv2json <b>-o</b> <i>file</i>
401<br><a href="dsv2json_out">#</a> dsv2json <b>--out</b> <i>file</i>
402
403Specify the output file name. Defaults to “-” for stdout.
404
405<a name="dsv2json_autotype" href="dsv2json_autotype">#</a> dsv2json <b>-a</b>
406<br><a href="dsv2json_autotype">#</a> dsv2json <b>--auto-type</b>
407
408Use type inference when parsing rows. See <a href="#autoType">d3.autoType</a> for how it works.
409
410<a name="dsv2json_input_delimiter" href="dsv2json_input_delimiter">#</a> dsv2json <b>-r</b> <i>delimiter</i>
411<br><a href="dsv2json_input_delimiter">#</a> dsv2json <b>--input-delimiter</b> <i>delimiter</i>
412
413Specify the input delimiter character. Defaults to “,” for reading CSV. (You can enter a tab on the command line by typing ⌃V.)
414
415<a name="dsv2json_input_encoding" href="dsv2json_input_encoding">#</a> dsv2json <b>--input-encoding</b> <i>encoding</i>
416
417Specify the input character encoding. Defaults to “utf8”.
418
419<a name="dsv2json_output_encoding" href="dsv2json_output_encoding">#</a> dsv2json <b>-r</b> <i>encoding</i>
420<br><a href="dsv2json_output_encoding">#</a> dsv2json <b>--output-encoding</b> <i>encoding</i>
421
422Specify the output character encoding. Defaults to “utf8”.
423
424<a name="dsv2json_newline_delimited" href="dsv2json_newline_delimited">#</a> dsv2json <b>-n</b>
425<br><a href="dsv2json_newline_delimited">#</a> dsv2json <b>--newline-delimited</b>
426
427Output [newline-delimited JSON](https://github.com/mbostock/ndjson-cli) instead of a single JSON array.
428
429<a name="csv2json" href="#csv2json">#</a> <b>csv2json</b> [<i>options…</i>] [<i>file</i>]
430
431Equivalent to [dsv2json](#dsv2json).
432
433<a name="tsv2json" href="#csv2json">#</a> <b>tsv2json</b> [<i>options…</i>] [<i>file</i>]
434
435Equivalent to [dsv2json](#dsv2json), but the [input delimiter](#dsv2json_input_delimiter) defaults to the tab character (\t).
436
437### json2dsv
438
439<a name="json2dsv" href="#json2dsv">#</a> <b>json2dsv</b> [<i>options…</i>] [<i>file</i>]
440
441Converts the specified JSON input *file* to DSV. If *file* is not specified, defaults to reading from stdin. For example, to convert to JSON to CSV:
442
443```
444json2csv < example.json > example.csv
445```
446
447Or to convert a newline-delimited JSON stream to CSV:
448
449```
450json2csv -n < example.ndjson > example.csv
451```
452
453<a name="json2dsv_help" href="json2dsv_help">#</a> json2dsv <b>-h</b>
454<br><a href="json2dsv_help">#</a> json2dsv <b>--help</b>
455
456Output usage information.
457
458<a name="json2dsv_version" href="json2dsv_version">#</a> json2dsv <b>-V</b>
459<br><a href="json2dsv_version">#</a> json2dsv <b>--version</b>
460
461Output the version number.
462
463<a name="json2dsv_out" href="json2dsv_out">#</a> json2dsv <b>-o</b> <i>file</i>
464<br><a href="json2dsv_out">#</a> json2dsv <b>--out</b> <i>file</i>
465
466Specify the output file name. Defaults to “-” for stdout.
467
468<a name="json2dsv_input_encoding" href="json2dsv_input_encoding">#</a> json2dsv <b>--input-encoding</b> <i>encoding</i>
469
470Specify the input character encoding. Defaults to “utf8”.
471
472<a name="json2dsv_output_delimiter" href="json2dsv_output_delimiter">#</a> json2dsv <b>-w</b> <i>delimiter</i>
473<br><a href="json2dsv_output_delimiter">#</a> json2dsv <b>--output-delimiter</b> <i>delimiter</i>
474
475Specify the output delimiter character. Defaults to “,” for writing CSV. (You can enter a tab on the command line by typing ⌃V.)
476
477<a name="json2dsv_output_encoding" href="json2dsv_output_encoding">#</a> json2dsv <b>--output-encoding</b> <i>encoding</i>
478
479Specify the output character encoding. Defaults to “utf8”.
480
481<a name="json2dsv_newline_delimited" href="json2dsv_newline_delimited">#</a> json2dsv <b>-n</b>
482<br><a href="json2dsv_newline_delimited">#</a> json2dsv <b>--newline-delimited</b>
483
484Read [newline-delimited JSON](https://github.com/mbostock/ndjson-cli) instead of a single JSON array.
485
486<a name="json2csv" href="#json2csv">#</a> <b>json2csv</b> [<i>options…</i>] [<i>file</i>]
487
488Equivalent to [json2dsv](#json2dsv).
489
490<a name="json2tsv" href="#json2tsv">#</a> <b>json2tsv</b> [<i>options…</i>] [<i>file</i>]
491
492Equivalent to [json2dsv](#json2dsv), but the [output delimiter](#json2dsv_output_delimiter) defaults to the tab character (\t).
Note: See TracBrowser for help on using the repository browser.