Convert JSON to CSV

Updated: Feb 21, 2022
 * Copyright (c) 2006-2022 North Concepts Inc.  All rights reserved.
 * Proprietary and Confidential.  Use is subject to license terms.
package com.northconcepts.datapipeline.examples.cookbook;


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 = ",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);, writer);


All Examples

Mobile Analytics