Examples >

Write to JSON Stream (simple)

This example shows how to how to write to a JSON stream in Java using the SimpleJsonWriter class. The demo code creates some test data in memory and writes it to a JSON stream. However, a JSON stream can be created from other input sources like CSV, Excel, etc.

This example can easily be modified to show how to read from a JSON stream.

Java Code Listing

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

import java.io.OutputStreamWriter;

import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.json.SimpleJsonWriter;
import com.northconcepts.datapipeline.memory.MemoryReader;

public class WriteToJsonStream {

     * Produces the following JSON (line breaks added for clarity)
     * [ { "stageName":"John Wayne", "realName":"Marion Robert Morrison", "gender":"male", "city":"Winterset", "balance":156.35 }, {
     * "stageName":"Spiderman", "realName":"Peter Parker", "gender":"male", "city":"New York", "balance":-0.96 } ]

    public static void main(String[] args) throws Throwable {

        Record record1 = new Record();
        record1.setField("stageName", "John Wayne");
        record1.setField("realName", "Marion Robert Morrison");
        record1.setField("gender", "male");
        record1.setField("city", "Winterset");
        record1.setField("balance", 156.35);

        Record record2 = new Record();
        record2.setField("stageName", "Spiderman");
        record2.setField("realName", "Peter Parker");
        record2.setField("city", "New York");
        record2.setField("balance", -0.96);

        MemoryReader reader = new MemoryReader(new RecordList(record1, record2));

        SimpleJsonWriter writer = new SimpleJsonWriter(new OutputStreamWriter(System.out));

        Job.run(reader, writer);


Code Walkthrough

  1. Two Record objects are created in memory with the fields stageName, realName,gender,city and balance set to some test values.
  2. These Record objects are added to a RecordList.
  3. A MemoryReader is created to read this RecordList.
  4. A new SimpleJsonWriter is created corresponding to the file path of the output JSON file credit-balance-04.json.
  5. Data is transferred from memory to the output JSON stream via JobTemplate.DEFAULT.transfer method.


A SimpleJsonWriter is an output writer that can be used to write to a JSON stream. It is a sub-class of TextWriter.html and overrides the open and close among other methods. It encapsulates all the details of writing data to a JSON stream, so the user need not write this code.
A JSON stream may also be created programmatically (refer create a JSON stream programmatically). This approach should be used when one needs more control over the JSON stream creation process.

Output JSON file

      "stageName":"John Wayne",
      "realName":"Marion Robert Morrison",
      "realName":"Peter Parker",
      "city":"New York",
Mobile Analytics