Sunday, August 9, 2015

Java: Compare two JSON strings


Following is the code to parse two json strings and compare them
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson3{
 
 private static String srcJsonStr = "SRC JSON String here";
 private static String destJsonStr = "Dest JSON String here";

 public static void main(String[] args){
  try{
   JSONObject srcJson = new JSONObject(srcJsonStr);
   JSONObject destJson = new JSONObject(destJsonStr);
   
   compareJson(srcJson, destJson);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void compareJson(JSONObject srcJson, JSONObject destJson){
  StringBuilder diffLog = new StringBuilder();
  
  try{
   Map srcJsonMap = new LinkedHashMap();
   Map destJsonMap = new LinkedHashMap();
   
   srcJsonMap = parseJson("", srcJson, srcJsonMap);
   destJsonMap = parseJson("", destJson, destJsonMap);
   
   //union
   Set union = new HashSet(srcJsonMap.keySet());
   union.addAll(destJsonMap.keySet());
   
   //h1-h2
   Set diff1 = new HashSet(srcJsonMap.keySet());
   diff1.removeAll(destJsonMap.keySet());
   
   //h1-h2
   Set diff2 = new HashSet(destJsonMap.keySet());
   diff2.removeAll(srcJsonMap.keySet());
   
   //intersection
   Set intersect = new HashSet(srcJsonMap.keySet());
   intersect.retainAll(destJsonMap.keySet());
   
   if(intersect.size() > 0){
    for(String key: intersect){
     Object srcData = srcJsonMap.get(key);
     Object destData = destJsonMap.get(key);
     
     if( ! srcData.toString().equals(destData.toString())){
      diffLog.append("KEY :: " + key + "\n");
      diffLog.append("SRC DATA :: " + srcData + "\n");
      diffLog.append("DEST DATA :: " + destData + "\n");
     }
    }
   }
   
   if(diff1.size() > 0){
    diffLog.append("\n");
    diffLog.append("###########################");
    diffLog.append("Only in SRC JSON");
    diffLog.append("###########################");
    diffLog.append("\n");
    for(String key: diff1){
     diffLog.append("KEY :: " + key + "\n");
     diffLog.append("DATA :: " + srcJsonMap.get(key) + "\n");
     diffLog.append("\n");
    }
   }
   
   if(diff2.size() > 0){
    diffLog.append("\n");
    diffLog.append("###########################");
    diffLog.append("Only in DEST JSON");
    diffLog.append("###########################");
    diffLog.append("\n");
    for(String key: diff2){
     diffLog.append("KEY :: " + key + "\n");
     diffLog.append("DATA :: " + destJsonMap.get(key) + "\n");
     diffLog.append("\n");
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  System.out.println(diffLog.toString());
 }
 
 public static Map parseJson(String key, JSONObject jsonObject, Map map){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map);
      }
      else{
       map.put(keyVal, jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj), map);
     }
     else{
      map.put(keyVal, jsonObject.get(obj));
     }
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  return map;
 }
}

Java: Store Json String into a Java Map


Following is the code to parse a json string and store the structure in Map
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson2{
 
 private static String jsonStr = "JSON String here";

 public static void main(String[] args){
  try{
   JSONObject obj = new JSONObject(jsonStr);
   parseJson(obj);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void parseJson(JSONObject jsonObject){
  Map map = new LinkedHashMap();
  
  try{
   parseJson("", jsonObject, map);
   
   for(String key: map.keySet()){
    System.out.println(key + "::" + map.get(key));
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static Map parseJson(String key, JSONObject jsonObject, Map map){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map);
      }
      else{
       map.put(keyVal, jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj), map);
     }
     else{
      map.put(keyVal, jsonObject.get(obj));
     }
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  return map;
 }
}

Java: parse a JSON string


Following is the code to parse a json string
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson1{
 
 private static String jsonStr = "";

 public static void main(String[] args){
  try{
   JSONObject obj = new JSONObject(jsonStr);
   parseJson("", obj);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void parseJson(String key, JSONObject jsonObject){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k));
      }
      else{
       System.out.println(keyVal + " :: " + jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj));
     }
     else{
      System.out.println(keyVal + " :: " + jsonObject.get(obj));
     }
    }
   }
   
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}