A command-line tool to convert CSV files to JSON format, supporting both file input and piped stdin.
- Convert CSV files to JSON with automatic type detection
- Support for piped stdin input (e.g.,
cat file.csv | ctj) - Support for numbers, booleans, and strings
- Pretty print JSON output
- Output to file or stdout
- Command-line interface with helpful options
cargo install ctjThis will install the latest version from crates.io. After installation, you can use ctj command directly from anywhere.
Clone the repository and install locally:
git clone https://github.com/lef237/ctj.git
cd ctj
cargo install --path .git clone https://github.com/lef237/ctj.git
cd ctj
cargo build --releaseThis creates an executable at ./target/release/ctj.
Convert CSV to JSON and output to stdout:
ctj input.csvOr using the explicit flag:
ctj -i input.csvYou can also pipe CSV data directly to ctj:
cat input.csv | ctj
echo "name,age\nJohn,30" | ctj
curl https://example.com/data.csv | ctjFormat JSON output with indentation:
ctj input.csv -pSave JSON output to a file:
ctj input.csv -o output.json-i, --input <FILE>: Input CSV file (optional, can also be provided as positional argument; if not provided, reads from stdin)-o, --output <FILE>: Output JSON file (optional, defaults to stdout)-p, --pretty: Pretty print JSON output-n, --no-header: Treat the first row as data, not headers (generates column_0, column_1, etc.)-h, --help: Show help message-V, --version: Show version information
Given a CSV file sample.csv:
name,age,city,active
John,25,Tokyo,true
Alice,30,Osaka,false
Bob,35,Kyoto,true
ctj sample.csvOutput:
[{"name":"John","age":25,"city":"Tokyo","active":true},{"name":"Alice","age":30,"city":"Osaka","active":false},{"name":"Bob","age":35,"city":"Kyoto","active":true}]ctj sample.csv -pOutput:
[
{
"name": "John",
"age": 25,
"city": "Tokyo",
"active": true
},
{
"name": "Alice",
"age": 30,
"city": "Osaka",
"active": false
},
{
"name": "Bob",
"age": 35,
"city": "Kyoto",
"active": true
}
]For CSV files without header rows, use the -n option to generate default column names:
ctj sample-no-header.csv -n -pGiven a CSV file sample-no-header.csv without headers:
,,
,,FALSE
,55.5,
Output:
[
{
"column_0": "",
"column_1": "",
"column_2": ""
},
{
"column_0": "",
"column_1": "",
"column_2": false
},
{
"column_0": "",
"column_1": 55.5,
"column_2": ""
}
]You can pipe CSV data directly into ctj:
# Basic piped input
cat sample.csv | ctj -p
# Processing data from a URL
curl -s https://example.com/data.csv | ctj --pretty
# Process data and save to file
echo "name,score\nAlice,95.5\nBob,87" | ctj > results.json
# Process without headers and pretty print
echo "John,30,Tokyo\nJane,25,Osaka" | ctj --no-header --prettyOutput for the last command:
[
{
"column_0": "John",
"column_1": 30,
"column_2": "Tokyo"
},
{
"column_0": "Jane",
"column_1": 25,
"column_2": "Osaka"
}
]The tool automatically detects and converts data types:
- Integers: Whole numbers (e.g.,
25,100) are detected as integers - Floating-point numbers: Numbers with decimal points (e.g.,
95.5,87.2) are detected as floats - Booleans:
true,false,TRUE,FALSE,True,Falseare converted to JSON booleans (case-insensitive) - Strings: All other values are treated as strings
This project is available under the MIT License.