June 20, 2020

Read Any Json Type File Using Java

There are various APIs and jars available that helps to read Json files in java.Here in this blog, I have used simplest way to read Json file.

I have used Jackson-Databind jar with version 2.2.3.

I have used STS as IDE, you can use eclipse to implement this program.Now, create new maven project or simple java project and create your choice of class.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Application_Starter {
     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
                File file = new File("C:\\\\Users\\\\username\\\\Desktop\\\\RESULT.json");
                ObjectMapper objectMapper = new ObjectMapper();
                final Map<String, Object> son = objectMapper.readValue(file,
                                new TypeReference<Map<String, Object>>() {}
                for (Entry<String, Object> entry : son.entrySet())  {
                                String Server_name = entry.getKey();
                                Object obj =   entry.getValue();

Add This code in your class and add your json file location in file(“—–“).

"S001": {
    "CPU_percentage" : 12 ,
    "Used_memory" : 16196
"S002": {
    "CPU_percentage" : 5 ,
    "Used_memory" : 15032
"S003": {
    "CPU_percentage" : 1 ,
    "Used_memory" : 11730
"S004": {
    "CPU_percentage" : 4 ,
    "Used_memory" : 11963
"S005": {
    "CPU_percentage" : 1 ,
    "Used_memory" : 11563
"S006": {
    "CPU_percentage" : 2 ,
    "Used_memory" : 9709

Add This Text to your RESULT.json file and save it.


Simply add this dependency in your pom.xml file.

If you have created simple java application you can download the jackson jar , I have provided the link on bottom of the page and directly add downloaded jar to your build path.

You can find build path by right click to your application and just add jar in dependency tab.

You will see output as :

Share your comment or problem if you face while setup the code.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *