Examples >

Convert JSON to CSV

 * Copyright (c) 2006-2020 North Concepts Inc.  All rights reserved.
 * Proprietary and Confidential.  Use is subject to license terms.
 * https://northconcepts.com/data-pipeline/licensing/
package com.northconcepts.datapipeline.examples.cookbook;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.csv.CSVWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.json.JsonReader;

public class ConvertJsonToCsv {

/*   Consumes the following JSON (line breaks added for clarity).  Yes, it starts with a new line an two slashes.    

// [
      "ltt":"4:00PM EDT",
      "lt":"Aug 1, 4:00PM EDT",
      "elt":"Aug 1, 7:54PM EDT",
      "ltt":"4:00PM EDT",
      "lt":"Aug 1, 4:00PM EDT",
      "elt":"Aug 1, 7:15PM EDT",
      "ltt":"4:00PM EDT",
      "lt":"Aug 1, 4:00PM EDT",

    public static void main(String[] args) throws Throwable {
    	String url = "http://www.google.com/finance/info?client=ig&q=msft,orcl,adbe";

    	BufferedReader input = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "UTF-8"));
        OutputStreamWriter output = new OutputStreamWriter(System.out);

        // remove preceding slashes from stream

    	DataReader reader = new JsonReader(input)
    	    .addField("symbol", "//array/object/t")
    	    .addField("exchange", "//array/object/e")
    	    .addField("price", "//array/object/l")
    	    .addField("change", "//array/object/c")
        DataWriter writer = new CSVWriter(output);
        Job.run(reader, writer);

Mobile Analytics