[![GitHub release](https://img.shields.io/github/release/apolitical/csv2json.svg)](https://github.com/apolitical/csv2json/releases) [![GitHub license](https://img.shields.io/github/license/apolitical/csv2json.svg)](https://github.com/apolitical/csv2json/blob/master/LICENSE) [![Crates.io](https://img.shields.io/crates/v/csv2json.svg)](https://crates.io/crates/csv2json) csv2json ======== Turns a CSV into a JSON file Installation: ------------- ``` $ cargo install csv2json ``` Usage: ------ ``` $ csv2json --in > ``` ### CSV Delimiter 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: ```csv colon:delimited one:two ``` Without specifying: ```json [ { "colon:delimited": "one:two" } ] ``` Using `-d :` ```json [ { "colon": "one", "delimited": "two" } ] ``` ### Dimensional Seperator If your CSV contains multidimensional data, you can add use the dimensional separator argument `-d` Eg: ```csv name.first,name.last,age Daniel,Mason,not telling ``` Without using the separator: ```json [ { "age": "not telling", "name.first": "Daniel", "name.last": "Mason" } ] ``` Setting the separator `-d .`: ```json [ { "name": { "first": "Daniel", "last": "Mason" }, "age": "not telling" } ] ``` ### Arrays You can use `--arrays` (or `-a`) with `-d` to break items into arrays ```csv name,pets.1,pets.2 Daniel Mason,Yuki,Tinky ``` Without using arrays: ```json [ { "name": "Daniel Mason", "pets.1": "Yuki", "pets.2": "Tinky" } ] ``` With arrays (`-d . -a`): ```json [ { "name": "Daniel Mason", "pets": [ "Yuki", "Tinky" ] } ] ``` **Note:** The number of the key is irrelevant, it only need be a number for example: ```csv name,pets.45,pets.22 Daniel Mason,,Tinky ``` Will produce: ```json [ { "name": "Daniel Mason", "pets": [ "", "Tinky" ] } ] ``` ### Remove Empty Strings 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. ```csv name.first,name.last,age,pets.1,pets.2 daniel,,34,, ``` ```shell $ csv2json --in test.csv -d . -a --remove-empty-strings ``` ```json [ { "age": "34", "name": { "first": "daniel" }, "pets": [] } ] ``` ### Remove Empty Objects 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. ```csv name.first,name.last,pets.1.name,pets.1.type,pets.2.name,pets.2.type james,smith,,,, daniel,mason,yuki,cat,tinky,cat ``` ```shell $ csv2json --in test.csv -d . -a --remove-empty-strings --remove-empty-objects ``` ```json [ { "name": { "first": "james", "last": "smith" }, "pets": [] }, { "name": { "first": "daniel", "last": "mason" }, "pets": [ { "name": "yuki", "type": "cat" }, { "name": "tinky", "type": "cat" } ] } ] ``` ### Output to directory Using the `--out-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`. ### Output to files based on names Using the `--out-name