Posted In: Java Core, JSON

Converting JSON to CSV in Java using org.json.CDL

Example first reads file and the creates JSONArray. After that it again creates CSV string and writes to a CSV file. package com.example.csv; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import org.apache.commons.io.FileUtils; import org.json.CDL; import org.json.JSONArray; import org.junit.Test; public class JsonToCsv { private StringBuilder sb = new StringBuilder(); @Test public void jsontocsv() throws Exception […]

Read More

Tags:

by , on August 31st, 2017

Posted In: GSON, JSON

How to use GSON streaming JsonReader to read big JSON file

Usecase – You have JSON data stored at a location. It is big so do not have luxury to read it fully in GSON or you just need use some part of JSON. GSON API provides streaming JsonReader class to do that. Maven Jackson 2X Classes used from GSON com.google.gson.stream.JsonReader Test data Output

Read More

Tags:

by , on August 8th, 2017

Posted In: Jackson 2X, JSON

How to use Jackson 2.X to read big JSON file

Usecase – You have JSON data stored at a location. It is big so do not have luxury to read it fully in ObjectMapper or you just need use some part of JSON. Jackson API provides streaming JSON parser class to do that. Maven Jackson 2X Classes used from Jackson 2X import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; […]

Read More

Tags:

by , on August 8th, 2017

Posted In: JSON

Ways to read json from a URL in Java – Using java.net.URL

  1. Using java.net.URL and org.apache.commons.io.IOUtils   MAVEN 2. Using java.net.URL and com.google.common.io.CharStreams   MAVEN 3. Using java.net.URL and java.io.BufferedReader   MAVEN

Read More

Tags: ,

by , on March 5th, 2017

Posted In: JSON

Convert json string to pretty print json output using JSON library

    1. Example will use following Test data   2. Using GSON.toJson   3. Using Jackson 2X writerWithDefaultPrettyPrinter   4. Using Jackson 1X writerWithDefaultPrettyPrinter   5. Using org.json.JSONObject.toString(indent) from org.json library   6. Using net.sf.json.JSONObject.toString(indent) from net.sf.json-lib library   7. Output { “portfolio” : [ { “stock” : “Infosys”, “quantity” : “100”, “buy” : […]

Read More

Tags: , , , , ,

by , on February 26th, 2017

Posted In: GSON, JSON

Check two JSON Objects for equality – GSON

  This example uses GSON library     Examples using GSON   1. Success test with a JSON string   2. Error test with a JSON string   3. Success test with a JSON string This test covers How do I compare 2 json files having same key-values but in different order in java?  

Read More

Tags: , ,

by , on February 24th, 2017

Posted In: JSON

Check two JSON Objects for equality – Jackson

  This example uses jackson library     Examples using Hamcrest-JSON   1. Success test with a JSON string   2. Error test with a JSON string   3. Success test with a JSON String This test covers How do I compare 2 json files having same key-values but in different order in java?   […]

Read More

Tags: , ,

by , on February 24th, 2017

Posted In: JSON

Check two JSON Objects for equality – Hamcrest-JSON

  This example uses hamcrest-json library     Examples using Hamcrest-JSON   1. Success test with a org.json.JSONObject   2. Error test with a org.json.JSONObject   3. Success test with a JSON String   4. Error test with a JSON String  

Read More

Tags: , , ,

by , on February 24th, 2017

Posted In: JSON

Check two JSON Objects for equality – JSONAssert

  This example uses JSONassert library     Examples using JSONassert   1. Success test with a org.json.JSONObject   2. Error test with a org.json.JSONObject   3. Success test with a JSON String   4. Error test with a JSON String  

Read More

Tags: ,

by , on February 24th, 2017

Posted In: JSON

How to transform JSON to another JSON format using JOLT

  This example uses JOLT library   You need input json and spec json   It has following Transforms shift : copy data from the input tree and put it the output tree default : apply default values to the tree remove : remove data from the tree sort : sort the Map key values […]

Read More

Tags: , ,

by , on February 24th, 2017

  • Categories