Read Big Decimal In JSON
Updated: May 31, 2022
This example shows you how to read big decimals form JSON file using Data Pipeline.
In this demo code you are going to use JsonRecordReader to read records which contains country, capital, language and XY coordinate fields from simple-json-to-file-with-bigdecimal.json
file and print them on the console via StreamWriter.newSystemOutWriter().
BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.
Input JSON file
[ { "country":"Philippines" ,"capital":"Manila" ,"language":"Filipino" ,"x-coodrinates" : 14.583331 ,"y-coodrinates" : 121.0 } ,{ "country":"Japan" ,"capital":"Tokyo" ,"language":"Japanese" ,"x-coodrinates" : 35.652832 ,"y-coodrinates" : 139.839478 } ]
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook; import java.io.File; import com.northconcepts.datapipeline.core.DataReader; import com.northconcepts.datapipeline.core.DataWriter; import com.northconcepts.datapipeline.core.StreamWriter; import com.northconcepts.datapipeline.job.Job; import com.northconcepts.datapipeline.json.JsonRecordReader; public class ReadBigDecimalInJSON { public static void main(String[] args) { DataReader reader = new JsonRecordReader(new File("example/data/input/simple-json-to-file-with-bigdecimal.json")) .addRecordBreak("/array/object").setUseBigDecimal(true); DataWriter writer = StreamWriter.newSystemOutWriter(); Job.run(reader, writer); } }
Code Walkthrough
- First, JsonRecordReader is created corresponding to the input JSON file
simple-json-to-file-with-bigdecimal.json
. By passingtrue
to setUseBigDecimal() method you are enablingJsonRecordReader
to useBigDecimal
which is disabled by default. In this exampleJsonRecordReader
is enabled to read XY coordinate values from the input JSON file as big decimals. - Data are transferred from JsonRecordReader to the console via Job.run() method. See how to compile and run data pipeline jobs
JsonRecordReader
JsonRecordReader is an input reader that can read records from an input JSON stream. A method JsonRecordReader.addRecordBreak
tells the reader to return a new record using whatever fields have been assigned. This method is basically used to demarcate records.
Console output
04:09:46,407 DEBUG [main] datapipeline:37 - DataPipeline v7.2.0-SNAPSHOT by North Concepts Inc. 04:09:52,630 DEBUG [main] datapipeline:615 - Job[1,job-1,Wed May 25 04:09:52 EAT 2022]::Start ----------------------------------------------- 0 - Record (MODIFIED) { 0:[country]:STRING=[Philippines]:String 1:[capital]:STRING=[Manila]:String 2:[language]:STRING=[Filipino]:String 3:[x-coodrinates]:BIG_DECIMAL=[14.583331]:BigDecimal 4:[y-coodrinates]:BIG_DECIMAL=[121]:BigDecimal } ----------------------------------------------- 1 - Record (MODIFIED) { 0:[country]:STRING=[Japan]:String 1:[capital]:STRING=[Tokyo]:String 2:[language]:STRING=[Japanese]:String 3:[x-coodrinates]:BIG_DECIMAL=[35.652832]:BigDecimal 4:[y-coodrinates]:BIG_DECIMAL=[139.839478]:BigDecimal } ----------------------------------------------- 2 records 04:10:00,157 DEBUG [main] datapipeline:661 - job::Success