Read a Simple JSON File

Updated: Aug 10, 2023

This example shows how to read records from JSON files, parse that JSON data, and convert it into object representations in a programming language. It simplifies the process of working with JSON data by abstracting away the complexities of manual parsing and object creation.

JSON data often needs to be processed, transformed, or analyzed in various applications. DataPipeline facilitates the reading of JSON objects, enabling you to access and manipulate the data as needed for tasks such as data transformation, analysis, reporting, or visualization.

This example can easily be modified to show how to write to a JSON file using SimpleJsonWriter class.


Input JSON file

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


Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;


import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.json.SimpleJsonReader;

public class ReadSimpleJsonFile {

    public static void main(String[] args) {
        DataReader reader = new SimpleJsonReader(new File("example/data/input/simple-json-input.json"));
       , new StreamWriter(System.out));


Code Walkthrough

  1. First, SimpleJsonReader object is created corresponding to the input file simple-json-input.json.
  2. Data is extracted from the JSON file via
  3. An array of objects is then printed on the console via StreamWriter class which takes System.out as a parameter to its constructor. Alternatively a static method StreamWriter.newSystemOutWriter() can also be passed to the constructor instead of new StreamWriter(System.out).

SimpleJsonReader and SimpleJsonWriter

SimpleJsonReader and SimpleJsonWriter classes assist in reading and writing objects from and to JSON files. In this demo code, only SimpleJsonReader is used to read a JSON stream created by SimpleJsonWriter but we can also use SimpleJsonWriter to write JSON stream to a file.


Console Output

0 - Record (MODIFIED) {
    0:[stageName]:STRING=[John Wayne]:String
    1:[realName]:STRING=[Marion Robert Morrison]:String

1 - Record (MODIFIED) {
    1:[realName]:STRING=[Peter Parker]:String
    3:[city]:STRING=[New York]:String

2 records
Mobile Analytics