Coder Social home page Coder Social logo

csv-to-json's Introduction

CSV to JSON

A simple CSV to JSON command line app written in Rust.

Usage

Usage: csv_to_json SOURCE_FILE_NAME [options]

Options:
    -o TARGET_FILE_NAME The path of the output file including the file
                        extension.
    -n, --null          Empty strings are set to null.
    -k, --keyed         Generate output as keyed JSON.
    -h, --help          Prints this help menu.

Installation / Compilation

I haven't made any binaries yet but you can easily compile it if you have Rust and Cargo installed. Pull the repository and call cargo run --release to compile.

Examples

The example below would parse the data inside sample.csv, convert to json, and write to output.json. The output file is created if it doesn't exist. Otherwise, the contents will be overwritten.

csv_to_json sample.csv -o output.json

If no output file is specified then the name of the source file will be used and will append ".json" to it. In the example below, sample.csv will generate sample.json.

csv_to_json sample.csv

Options

-n, --null

-n, --null          Empty strings are set to null.

If the above option is specified then all empty data would have a null value as shown below.

textID,english,tagalog
GUESS_THE_INGREDIENT,Guess the Ingredient,Hulaan ang Ingredient
GUESS_THE_CHEF,,

Would convert to:

[
    {
        "textID":"GUESS_THE_INGREDIENT",
        "english":"Guess the Ingredient",
        "tagalog":"Hulaan ang Ingredient"
    },
    {
        "textID":"GUESS_THE_CHEF",
        "english":null,
        "tagalog":null
    }
]

If -n is not specified then it will show a "" instead of null as shown below:

[
    {
        "textID":"GUESS_THE_INGREDIENT",
        "english":"Guess the Ingredient",
        "tagalog":"Hulaan ang Ingredient"
    },
    {
        "textID":"GUESS_THE_CHEF",
        "english":"",
        "tagalog":""
    }
]

-k, --k

-k, --keyed         Generate output as keyed JSON.

If the above option is set then the json structure would be a Keyed JSON structure. The example below shows what it looks like:

textID,english,tagalog
GUESS_THE_INGREDIENT,Guess the Ingredient,Hulaan ang Ingredient
GUESS_THE_CHEF,,
GUESS_THE_DISH,Guess the Dish,Hulaan ang luto
{
    "GUESS_THE_INGREDIENT":{
        "english":"Guess the Ingredient",
        "tagalog":"Hulaan ang Ingredient"
    },
    "GUESS_THE_CHEF":{
        "english":"",
        "tagalog":""
    },
    "GUESS_THE_DISH":{
        "english":"Guess the Dish",
        "tagalog":"Hulaan ang luto"
    }
}

Compare the above with the a non-keyed JSON structure as shown below:

[
    {
        "textID":"GUESS_THE_INGREDIENT",
        "english":"Guess the Ingredient",
        "tagalog":"Hulaan ang Ingredient"
    },
    {
        "textID":"GUESS_THE_CHEF",
        "english":"",
        "tagalog":""
    },
    {
        "textID":"GUESS_THE_DISH",
        "english":"Guess the Dish",
        "tagalog":"Hulaan ang luto"
    }
]

Contributing

All contributions are welcome!

TODO

  • Convert into a lib
  • Provide downloadable releases

csv-to-json's People

Contributors

accidentalrebel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.