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",
        "gender":"male",
        "city":"Winterset",
        "balance":156.35
    },
    {
        "stageName":"Spiderman",
        "realName":"Peter Parker",
        "gender":"male",
        "city":"New York",
        "balance":-0.96
    }
]

 

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

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"));
        
        Job.run(reader, 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 Job.run().
  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
    2:[gender]:STRING=[male]:String
    3:[city]:STRING=[Winterset]:String
    4:[balance]:DOUBLE=[156.35]:Double
}

-----------------------------------------------
1 - Record (MODIFIED) {
    0:[stageName]:STRING=[Spiderman]:String
    1:[realName]:STRING=[Peter Parker]:String
    2:[gender]:STRING=[male]:String
    3:[city]:STRING=[New York]:String
    4:[balance]:DOUBLE=[-0.96]:Double
}

-----------------------------------------------
2 records
Mobile Analytics