1. 2017-07-21 - Minify JSON; Tags: Minify JSON
    Loading...

    Minify JSON

    Another way to minify JSON is to use the npm package.

    Installation

    npm install -g json-minify
    

    Usage

    Having a test-data.json file

    {
      "friends": [
        {
          "id": 0,
          "name": "Amparo Henry"
        },
        {
          "id": 1,
          "name": "Stone Ferguson"
        },
        {
          "id": 2,
          "name": "Cheryl Rocha"
        }
      ]
    }
    

    Defaults to stdout

    tan@omega:~$ json-minify test-data.json
    {"friends":[{"id":0,"name":"Amparo Henry"},{"id":1,"name":"Stone Ferguson"},{"id":2,"name":"Cheryl Rocha"}]}
    

    Write to new file

    json-minify test-data.json > minified.json
    

    Or use it as pipe

    tan@omega:~$ curl -s http://elasticsearch:9200 | json-minify
    {"name":"dev","cluster_name":"fo-test","cluster_uuid":"sE_HoGSZRKyhGHN9lk2gSA","version":{"number":"5.4.3","build_hash":"eed30a8","build_date":"2017-06-22T00:34:03.743Z","build_snapshot":false,"lucene_version":"6.5.1"},"tagline":"You Know, for Search"}
    
  2. 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
    
  3. 2016-02-15 - Code Review - Boolean's Delight; Tags: Code Review - Boolean's Delight
    Loading...

    Code Review - Boolean's Delight

    Did some code review and saw some fancy lines :-( It is about JSON handling with Jackson. Basically is about how to determine that a given JsonNode is not null and a container.

    Snippet

    /**
     * Check if {@link JsonNode} is not null and a container. (includes more JsonNodes)
     * @param jsonNode
     * @return true / false
     */
    public static boolean isJsonNodeValidAndContainer(JsonNode jsonNode) {
    	return jsonNode == null || !jsonNode.isContainerNode() ? false : true;
    }
    

    The intent is to avoid the necessary null check in each invocation. Improvement 1: Since it is a condition, just return the result. The ? is superfluous.

    /**
     * Check if {@link JsonNode} is not null and a container. (includes more JsonNodes)
     * @param jsonNode
     * @return true / false
     */
    public static boolean isJsonNodeValidAndContainer(JsonNode jsonNode) {
    	return (jsonNode == null || !jsonNode.isContainerNode());
    }
    

    What I personally don’t like, is the method name. It should be obvious and should have a single purpose. isValidContainer or isValidContainerNode (including the null check) sounds more reasonable to me. The implementation of isContainerNode()

    @Override
    public final boolean isContainerNode() {
    	final JsonNodeType type = getNodeType();
    	return type == JsonNodeType.OBJECT || type == JsonNodeType.ARRAY;
    }
    

    See also the the current latest API doc.

  4. 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
    
  5. 2015-06-18 - Jolokia - JMX with JSON over HTTP; Tags: Jolokia - JMX with JSON over HTTP
    Loading...

    Jolokia - JMX with JSON over HTTP

    Jolokia is remote JMX with JSON over HTTP. It provides several agents to expose JMX MBeans. OSGi is one of them :smile:. Using DropWizard Metrics with the JmxReporter, you can easily access the metrics. If you use the Java EE agent (deploy the jolokia.war to your Java EE application server), it enables you to access the metric under the given MBeans objectName, e.g. The context-root can be simplified to jolokia via the web.xml.

    curl http://localhost:8080/jolokia-war-1.3.1/read/metrics:name=trx.process.approved
    • read is the operation
    • metrics:name=trx.process.approved the JMX objectName of the MBean

    Example output of processed transaction that haven been approved.

    class="language-json">{ "request": { "mbean": "metrics:name=trx.process.approved", "type": "read" }, "value": { "Count": 2 }, "timestamp": 1434618683, "status": 200 }