Crates.io | csv2json |
lib.rs | csv2json |
version | 0.3.1 |
source | src |
created_at | 2018-03-28 17:19:30.371324 |
updated_at | 2019-03-08 16:32:27.40702 |
description | Converts CSVs into JSON documents |
homepage | |
repository | https://github.com/apolitical/csv2json |
max_upload_size | |
id | 57935 |
size | 39,567 |
Turns a CSV into a JSON file
$ cargo install csv2json
$ csv2json --in <csv file> > <json file>
By default, the csv is split by commas. If your csv is delimited in a different way, you can
specify the character using the --delimiter
or -d
option
Eg:
colon:delimited
one:two
Without specifying:
[
{
"colon:delimited": "one:two"
}
]
Using -d :
[
{
"colon": "one",
"delimited": "two"
}
]
If your CSV contains multidimensional data, you can add use the dimensional separator argument -d
Eg:
name.first,name.last,age
Daniel,Mason,not telling
Without using the separator:
[
{
"age": "not telling",
"name.first": "Daniel",
"name.last": "Mason"
}
]
Setting the separator -d .
:
[
{
"name": {
"first": "Daniel",
"last": "Mason"
},
"age": "not telling"
}
]
You can use --arrays
(or -a
) with -d
to break items into arrays
name,pets.1,pets.2
Daniel Mason,Yuki,Tinky
Without using arrays:
[
{
"name": "Daniel Mason",
"pets.1": "Yuki",
"pets.2": "Tinky"
}
]
With arrays (-d . -a
):
[
{
"name": "Daniel Mason",
"pets": [
"Yuki",
"Tinky"
]
}
]
Note: The number of the key is irrelevant, it only need be a number for example:
name,pets.45,pets.22
Daniel Mason,,Tinky
Will produce:
[
{
"name": "Daniel Mason",
"pets": [
"",
"Tinky"
]
}
]
You can remove empty strings from objects and arrays with the --remove-empty-strings
flag.
Note: this happens for both objects and arrays, which may have undesirable affects.
name.first,name.last,age,pets.1,pets.2
daniel,,34,,
$ csv2json --in test.csv -d . -a --remove-empty-strings
[
{
"age": "34",
"name": {
"first": "daniel"
},
"pets": []
}
]
You can remove empty objects from objects and arrays with the --remove-empty-objects
flag.
Note: this happens for both objects and arrays, which may have undesirable affects.
name.first,name.last,pets.1.name,pets.1.type,pets.2.name,pets.2.type
james,smith,,,,
daniel,mason,yuki,cat,tinky,cat
$ csv2json --in test.csv -d . -a --remove-empty-strings --remove-empty-objects
[
{
"name": {
"first": "james",
"last": "smith"
},
"pets": []
},
{
"name": {
"first": "daniel",
"last": "mason"
},
"pets": [
{
"name": "yuki",
"type": "cat"
},
{
"name": "tinky",
"type": "cat"
}
]
}
]
Using the --out-dir <dir>
to write the .json
file to the output dir. It will use the name of the
original file so --in /some/dir/my-data.csv --out-dir /some/other/dir
will produce the file
/some/other/dir/my-data.json
.
Using the --out-name <template>
with --out-dir <dir>
to write multiple files of json using the
template to generate their name from the original data. For example
Given test.csv
name.first,name.last,pets.1.name,pets.1.type,pets.2.name,pets.2.type
james,smith,suki,cat,,
daniel,mason,yuki,cat,tinky,cat
Running csv2json with the following naming template
$ csv2json --in test.csv --out-dir . --out-name "{name.first}-{name.last}" -d . -a --remove-empty-strings --remove-empty-objects
Will produce the following files
james-smith.json
{
"name": {
"first": "james",
"last": "smith"
},
"pets": [
{
"name": "suki",
"type": "cat"
}
]
}
daniel-mason.json
{
"name": {
"first": "daniel",
"last": "mason"
},
"pets": [
{
"name": "yuki",
"type": "cat"
},
{
"name": "tinky",
"type": "cat"
}
]
}
You can specify a column contains a boolean value by using the --boolean
option
type,option.a,option.b,option.c,option.d
true,1,true,anything,TRUE
false,0,false,,FALSE
$ csv2json --in test.csv -d . --boolean option.a --boolean option.b --boolean option.c --boolean option.d
[
{
"option": {
"a": true,
"b": true,
"c": true,
"d": true
},
"type": "true"
},
{
"option": {
"a": false,
"b": false,
"c": false,
"d": false
},
"type": "false"
}
]
You can specify a column contains a numeric value by using the --numeric
option
number
0
1
-1
1.0
$ csv2json --in test.csv --numeric number
[
{
"number": 0
},
{
"number": 1
},
{
"number": -1
},
{
"number": 1.0
}
]