1. 2017-07-21 - Convert JSON 2 CSV in JavaScript; Tags: Convert JSON 2 CSV in JavaScript
    Loading...

    Convert JSON 2 CSV in JavaScript

    json2csv is java script library which allows conversion of the format csv and json on a linux terminal.

    Installation

    Install it with npm

    npm -g json2csv
    

    CLI

    A quick demonstration

    test-data.json

    [
      {
        "_id": "5971ebce9b51d3f1afee6a34",
        "index": 0,
        "guid": "c48c4f70-e825-4be9-a1a9-63a145dc469d",
        "isActive": false
      },
      {
        "_id": "5971ebce3dc681e6c106cca5",
        "index": 1,
        "guid": "3f3ad5d9-f59b-4f79-92c6-b589fc29a3a5",
        "isActive": false
      },
      {
        "_id": "5971ebce96c43c94c6b984de",
        "index": 2,
        "guid": "ac24adb1-1c74-4f1f-ba6c-c108f76941f4",
        "isActive": false
      }
    ]
    

    Export to CSV

    $ json2csv -i test-data.json -o output.csv
    

    The above input will result in this output:

    "_id","index","guid","isActive"
    "5971ebce9b51d3f1afee6a34",0,"c48c4f70-e825-4be9-a1a9-63a145dc469d",false
    "5971ebce3dc681e6c106cca5",1,"3f3ad5d9-f59b-4f79-92c6-b589fc29a3a5",false
    "5971ebce96c43c94c6b984de",2,"ac24adb1-1c74-4f1f-ba6c-c108f76941f4",false
    
  2. 2015-10-08 - Convert from CSV to JSON with logstash; Tags: Convert from CSV to JSON with logstash
    Loading...

    Convert from CSV to JSON with logstash

    logstash allows with various plugins a quick solution, to convert CSV input to JSON output.

    This test configuration demonstrates a simple example with nested properties.

    # convert from csv to json
    input {
        generator {
            type => 'csv'
            message => '2015-10-08,CH,4711'
            count => 1
        }
    }
    filter {
        csv {
            columns => ['date', 'countryCode', 'value']
        }
        mutate {
            rename => {
                "date" => "[payment][date]"
                "countryCode" => "[payment][origin]"
                "value" => "[payment][value]"
            }
        }
    }
    output {
        stdout {
            codec => rubydebug{}
        }
    }
    output {
        stdout {
            codec => json{}
        }
    }
    
    • With the generator input plugin, we generate a CSV line.
    • The csv filter plugin, allows the parsing of the CSV data.
    • The mutate filter plugin, allows with rename to nest the properties under payment.
    • The output contains two definitions, the rubydebug and the JSON output (pretty printed by me).

    The output

    Logstash startup completed
    {
           "message" => [
            [0] "2015-10-08,CH,4711"
        ],
          "@version" => "1",
        "@timestamp" => "2015-10-08T14:31:03.344Z",
              "type" => "csv",
              "host" => "cinhtau",
          "sequence" => 0,
           "payment" => {
              "date" => "2015-10-08",
            "origin" => "CH",
             "value" => "4711"
        }
    }
    {
        "message":["2015-10-08,CH,4711"],
        "@version":"1",
        "@timestamp":"2015-10-08T14:31:03.344Z",
        "type":"csv",
        "host":"cinhtau",
        "sequence":0,
        "payment":{
            "date":"2015-10-08",
            "origin":"CH",
            "value":"4711"
        }
    }
    Logstash shutdown completed