csv2jsoncmd - CSV to JSON Converter¶
Description¶
Use this command line tool to convert CSV files to JSON format.
Usage¶
csv2jsoncmd [-h] [-i] [-o] [-e] [-N] [-d] [-sk] [-c] [-L] [-k] [-A] [-P] [-d] [-T] [-f] [-ex] [-nf] [-nx] [-l] [-V]
Optional Arguments¶
Arguments available via the --help
flag:
-h, --help Show this help message and exit
-i, --infile JSON input file, URL, or folder
-o, --outfile Output file, console if not specified
-e, --encoding Set the encoding type.
-ho, --headover Override CSV header with comma separated heading
-N, --noheader No heading in CSV input file
-d, --delimiter Delimiter of csv input, use -T for tab
-sk, --skip Skip N top lines in input file
-c, --config Config filename to load and use.
-L, --lines Generate Output file as JSON Lines format
-k, --key Generate keyed JSON, pass column # to identify column
-t, --topname Identify top level property name that appears at top of output.
-A, --array Generate JSON arrays
-P, --pretty Pretty format output
-T, --tab Tab is input delimiter. Overrides -d
-f, --fields Fields to include, starting at 1, ex. 3,2,1
-ex, --exclude Fields to exclude, starting at 1, ex. 3-5
-nf, --nulls Fields to convert NULL string to null, starting at 1, ex. 3,2,1
-nx, --exnulls Fields to not convert NULL string to null, starting at 1, ex. 3-5
-l, --limit Limit total lines written to output.
-V, --verbose Output helpful messages
-i –-infile FILENAME¶
You may pass the tool either a file name or web url and you must use
the -i
or --infile
parameter followed by the input as shown in the below. In the first example, we are
loading a .json file that is saved in the c:data folder. In the second example, we are loading directly from an example
website URL. If you don’t specify a data source, then stdin is used.:
csv2jsoncmd -i c:\data\example.csv
csv2jsoncmd -i http://www.example.com/example.csv`
-o –-outfile FILENAME¶
The -o
or --outfile
parameter sets the file in which to save the converted results. If no parameter is set,
then the output is written to the screen (stdout):
csv2jsoncmd -i c:\example.csv -o c:\converted.json
csv2jsoncmd -i http://www.example.com/example.csv -o c:\converted.json
-e –-encoding ENCODING¶
In order to specify an encoding type for the file, pass the -e
or --encoding
parameter. The most common
encoding types are UTF-8 and UTF-16. If nothing is passed, the software will guess the encoding.
csv2jsoncmd -i c:\example.csv -e utf-8
csv2jsoncmd -i c:\example.csv --encoding utf-16
-d –-delimiter DELIMITER¶
The -D
or --delimiter
flag will allow the user to pass the type of delimiter in the CSV file.
By default, the delimiter is determined for you.:
csv2jsoncmd -i c:\example.csv -d ;
In order to pass the pipe |as the delimiter, use -d "|" as the parameter:
csv2jsoncmd -i c:\example.csv -d "|"
In order to pass Tab as the delimiter, use -T as the parameter to override the delimiter:
csv2jsoncmd -i c:\example.csv -T
-ho –-headover¶
The JSON keys are based on the CSV column headers, so you can use this to override the headers and hence change the JSON keys:
csv2jsoncmd -i example.csv -ho pk,company,charge,comment
-N –-noheader¶
This flag indicates the input CSV file has no headers. Without this flag we assume a header row is present. If the input has no column headings, then we create our own column headers for the JSON output:
csv2jsoncmd -i c:\example.csv -N
-c -–config CONFIG_FILENAME¶
The configuration file used in the conversion is either created at runtime, or passed to the converter before the
conversion begins. The -c
or --config
flag will load a configuration file to used if a path is passed:
csv2jsoncmd -i c:\example.csv -c c:\config\myconfig.txt
-L -–lines¶
The -L
or --lines
flag will generate the output file using the JSON lines format:
csv2jsoncmd -i c:\example.csv -L
-k –-key KEY¶
The -k
or --key
flag will generate a keyed JSON file based on the key passed via the flag, pass a column
number to identify a column. You want to choose a column that is unique.:
csv2jsoncmd -i c:\example.csv -k "valueA"
-t –-topname TOPNAME¶
The -i
or --topname
will add a top level property name to the output
:
csv2jsoncmd -i c:\example.csv -t users
-A –-array¶
The -A
or --array
flag will generate JSON arrays based on the file structure:
csv2jsoncmd -i c:\example.csv -A
-P -–pretty¶
The -P
or --pretty
flag will format the output into a pretty format:
csv2jsoncmd -i c:\example.csv -P
-f –-fields FIELD_SPEC¶
The -f
or --fields
flag allows you to specifically set which field columns to include.
You can either specify the column number (starting at 1) or the field name based on the header.
You can use a range with field numbers, i.e. 2-6 , which indicates fields 2 through 6.:
csv2jsoncmd -i c:\example.csv -f 3,2,1
-ex -–exclude FIELD_SPEC¶
The -ex
or --exclude
flag allows you to specifically set which field columns to exclude.
The field list starts with 1. In the example below, we are excluding field 3,4, and 5.:
csv2jsoncmd -i c:example.csv -ex 3-5
-nf –-nulls FIELD_SPEC¶
The -nf
or --nulls
flag allows you to specifically set which field columns to include when converting a NULL or null string to JSON null. By default all fields are converted.
You can either specify the column number (starting at 1) or the field name based on the header.
You can use a range with field numbers, i.e. 2-6 , which indicates fields 2 through 6.:
csv2jsoncmd -i c:\example.csv -nf 3,2,1
-nx –-exnulls FIELD_SPEC¶
The -ex
or --exnulls
flag allows you to specifically set which field columns to exclude automatic conversion of NULL or null to a null JSON value.
The field list starts with 1. In the example below, we are excluding field 3,4, and 5.:
csv2jsoncmd -i c:\example.csv -nx 3-5
-l -–limit N¶
The -l
or --limit
flag will limit the number of rows used in the CSV file.
If no parameter is passed, the converter will write the entire file.
This value must be a number. This parameter will not affect the headers.
csv2jsoncmd -i c:\example.csv -l 10
-V –-verbose¶
The -V
or --verbose
flag will output useful messages as the program runs:
csv2jsoncmd -i c:\example.csv -V