How to Read JSON file in Java using JSON Simple Jar ?

How to Read JSON file in Java using JSON Simple Jar ? JSON file can be read using any JSON jars. Here we are going to read the JSON file in java using the json-simple-1.1 jar.

 

JSON-Simple jar and the sample json file used to read in java is attached at the last of this post.

 

Requirements:

json-simple-1.1.jar
[This jar file is shared at the last of the post]

 

Steps in overview:

  • Create JSONParser Object.
  • Parse the input json file using the parse method of JSONParser object, which returns the jsonarray.
  • JsonObject is retrieved from the jsonarray.
  • From the jsonobject pass the key and retrieve the inner json array.
  • Iterate inner json array and use the .get() method to get the required value by passing the key.

 

 

Sample JSON Input File:

[plain]

[{“lastBuildDate”:”2016-09-29 06:23:06″},{“item”:[{“Id”:”4c309ed042d82fd013366e0cd498c15a”,”DealTitle”:”Sale – Get Upto 60% Off On UCB Products”,”DealType”:”Offer”,”DealCode”:””,”DealDescription”:”Sale – By and get upto 60% off on UCB products at Myntra. No coupon code required. Here you can choose apparel footwear & accessories by UCB. Valid on products displayed on the landing page.”,”DealLink”:”http:\/\/www.s2d6.com\/x\/?x=c&z=s&v=6761371&k=[NETWORKID]&t=http%3A%2F%2Fwww.myntra.com%2Funited-colors-of-benetton%3Ff%3Ddiscount%3A40.0%26utm_source%3DDGM%26utm_medium%3Daffiliate”,”CouponCategory”:”All”,”CouponCategoryId”:”0″,”StoreName”:”Myntra”,”StoreId”:”20″,”CampaignLogo”:”http:\/\/dzh2xn6ml8v5t.cloudfront.net\/logo\/store\/20.png”,”LastVerified”:”2015-12-17 14:43:16″,”Expired”:”N%2FA”,”Exclusive”:”0″,”Thumbnail”:”http:\/\/dzh2xn6ml8v5t.cloudfront.net\/thumb\/store\/20.jpg”}]}]

[/plain]

complete samplejson.json file is attached at the last of this post.

 

 

Read JSON File – Java Source Code:

Change the path of the json file(samplejson.json) and run the below program.

[java]

package in.javadomain;

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class ReadJsonFile {

public static void main(String[] args) {
System.out.println(“JSON File Reading Started”);
JSONParser jsonParser = new JSONParser();
String jsonDealTitle, jsonStoreNm, jsonDealCd, jsonDealLink, jsonDealDesc, jsonExpiration, jsonDealType;
try {
JSONArray jsonArrayOuter = (JSONArray) jsonParser.parse(new FileReader(
“E:/samplejson.json”));
for (Object outerObj : jsonArrayOuter) {
JSONObject jsonObj = (JSONObject) outerObj;

JSONArray jsonArrayInner = (JSONArray) jsonObj.get(“item”);
if (jsonArrayInner != null) {
for (Object ob : jsonArrayInner) {
System.out.println(“Json Started”);
JSONObject eachEntry = (JSONObject) ob;

jsonStoreNm = eachEntry.get(“StoreName”).toString()
.trim();
jsonDealTitle = eachEntry.get(“DealTitle”).toString();
jsonDealCd = eachEntry.get(“DealCode”).toString();
jsonDealDesc = eachEntry.get(“DealDescription”)
.toString();
jsonDealType = eachEntry.get(“DealType”).toString();
jsonDealLink = eachEntry.get(“DealLink”).toString();
jsonExpiration = eachEntry.get(“Expired”).toString();

System.out.println(jsonStoreNm);
System.out.println(jsonDealTitle);
System.out.println(jsonDealDesc);
System.out.println(jsonDealType);
System.out.println(jsonDealLink);
System.out.println(jsonExpiration);
System.out.println(“Json Done”);
System.out.println(“\n\n\n”);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(“JSON File Reading Completed”);
}
}

[/java]

 

Console Output:

[plain]
JSON File Reading Started
Json Started
Myntra
Sale – Get Upto 60% Off On UCB Products
Sale – By and get upto 60% off on UCB products at Myntra. No coupon code required. Here you can choose apparel footwear & accessories by UCB. Valid on products displayed on the landing page.
Offer
http://www.s2d6.com/x/?x=c&z=s&v=6761371&k=[NETWORKID]&t=http%3A%2F%2Fwww.myntra.com%2Funited-colors-of-benetton%3Ff%3Ddiscount%3A40.0%26utm_source%3DDGM%26utm_medium%3Daffiliate
N%2FA
Json Done
Json Started
Myntra
Get Upto 50% Off On Apparels By Pepe Jean
Buy and get upto 50% off on apparels for men & women by pepe jean at Myntra. No coupon code required. Offer valid for limited period only. Offer valid on products displayed on the landing page.
Offer
http://www.s2d6.com/x/?x=c&z=s&v=6761371&k=[NETWORKID]&t=http%3A%2F%2Fwww.myntra.com%2Fpepe-40%3Futm_source%3DDGM%26utm_medium%3Daffiliate
N/A
Json Done
JSON File Reading Completed

[/plain]

 

Download samplejson.json file

Feel free to share any thoughts/comments in the below comments section.

Leave a Reply