alexwennerberg / json2csv

Convert JSON to CSV in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json2csv

ALPHA

Introduction

I love using the Javascript json2csv library. It's a great tool for following the Data Science at the Command Line philosophy. I wanted to attempt a rewrite in Rust in order to improve the performance and add some features I have missed.

json2csv 0.1.0
Alex Wennerberg <alex@alexwennerberg.com>
Converts JSON into CSV

USAGE:
    json2csv [FLAGS] [OPTIONS] [--] [INPUT]

FLAGS:
    -F, --flatten    Flatten nested jsons and arrays
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -f, --fields <fields>...       Optionally specify fields to include
    -d, --delimiter <delimiter>    Optionally specify delimiter to use. Use $'\t' for tab. If not specified, uses comma.
    -o, --output <output>          Output file. If not present, writes to stdout
    -N, --sample-lines <samples>   Analyze first N number of lines for header fields, default to 1
    -U, --unwind-on <unwind-on>    Unwind an array into multiple keys, similar to mongo

ARGS:
    <INPUT>    Input file. If not present, reads from stdin

Installation

This tool is still in alpha. Install from cargo:

cargo install json2csv

Or build from source:

git clone git://github.com/alexwennerberg/json2csv
cd json2csv
cargo build --release

Note that if you are using multiple tools named json2csv, you'll want to uninstall one of them or add an alias, such as json2csv-rs

Usage

For simple, flat jsons with a uniform structure, simply run json2csv input.json

If your json structure is not uniform, you may want use -N option such as json2csv input.json -N #num, which will use the first N number of lines to detect headers.

If your json is nested, you may want to use jq to do some pre-processing. --flatten will flatten all nested arrays in a json, such that they will have the format field.nested_field or field.nested_array.0, etc. Combine this with --get-headers to get all nested values.

You can use the --unwind-on option to "unwind" the json on a key. That is to say, to split a record that contains an array into an array of records containing each value of that array.

I don't include any of the formatting options that are present in the Javascript json2csv. This is following the Do One Thing and Do It Well principle -- this should just convert JSON to CSV, for any sort of reformatting or post-processing you can pipe the data into BurntSushi's excellent xsv library.

Submit a pull request for any feature requests!

About

Convert JSON to CSV in Rust

License:MIT License


Languages

Language:Rust 94.7%Language:Shell 5.3%