0

I want to compare two big json nodes in java and if they are different I need to print the value which was distinct in both nodes. JsonNode.equals() returns false if nodes are unequal but it does not tell the particular distinct value.

Thanks.

code example:

    import org.codehaus.jackson.JsonNode; 
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.util.JSONPObject;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.Map;


    public class TestJsonComp
    {
        static String json = "{\n" +
            "  \"d\" :\n" +
            "  {\n" +
            "    \"results\" :\n" +
            "    [\n" +
            "      {\n" +
            "        \"__metadata\" :\n" +
            "        {\n" +
            "          \"id\" :               \"            ##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" +
    "          \"uri\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" +
    "          \"type\" : \".submitResponse\",\n" +
    "          \"etag\" : \"W/\\\"##RequisitionUniqueName##\\\"\"\n" +
    "        },\n" +
    "        \"status\" : \"ERROR\",\n" +
    "        \"uniqueName\" : \"##RequisitionUniqueName##\",\n" +
    "        \"errors\" :\n" +
    "        {\n" +
    "          \"results\" :\n" +
    "          [\n" +
    "            {\n" +
    "              \"__metadata\" :\n" +
    "              {\n" +
    "                \"id\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" +
    "                \"uri\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" +
    "                \"type\" : \".errors\",\n" +
    "                \"etag\" : \"W/\\\"0\\\"\"\n" +
    "              },\n" +
    "              \"id\" : 0,\n" +
    "              \"error_description\" : \"title.\"\n" +
    "            },\n" +
    "            {\n" +
    "              \"__metadata\" :\n" +
    "              {\n" +
    "                \"id\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" +
    "                \"uri\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" +
    "                \"type\" : \".errors\",\n" +
    "                \"etag\" : \"W/\\\"1\\\"\"\n" +
    "              },\n" +
    "              \"id\" : 1,\n" +
    "              \"error_description\" : \"Line item 1 must be set.\"\n" +
    "            }\n" +
    "          ]\n" +
    "        }\n" +
    "      }\n" +
    "    ]\n" +
    "  }\n" +
    "}";

        static String jsonCompare = "{\n" +
    "  \"d\" :\n" +
    "  {\n" +
    "    \"results\" :\n" +
    "    [\n" +
    "      {\n" +
    "        \"__metadata\" :\n" +
    "        {\n" +
    "          \"id\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" +
    "          \"uri\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" +
    "          \"type\" : \".submitResponse\",\n" +
    "          \"etag\" : \"W/\\\"##RequisitionUniqueName##\\\"\"\n" +
    "        },\n" +
    "        \"status\" : \"ERROR\",\n" +
    "        \"uniqueName\" : \"##RequisitionUniqueName##\",\n" +
    "        \"errors\" :\n" +
    "        {\n" +
    "          \"results\" :\n" +
    "          [\n" +
    "            {\n" +
    "              \"__metadata\" :\n" +
    "              {\n" +
    "                \"id\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" +
    "                \"uri\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" +
    "                \"type\" : \".errors\",\n" +
    "                \"etag\" : \"W/\\\"0\\\"\"\n" +
    "              },\n" +
    "              \"id\" : 0,\n" +
    "              \"error_description\" : \"title.\"\n" +
    "            },\n" +
    "            {\n" +
    "              \"__metadata\" :\n" +
    "              {\n" +
    "                \"id\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" +
    "                \"uri\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" +
    "                \"type\" : \".errors\",\n" +
    "                \"etag\" : \"W/\\\"1\\\"\"\n" +
    "              },\n" +
    "              \"id\" : 1,\n" +
    "              \"error_descriptio\" : \"Line item 1 must be set.\"\n" +
    "            }\n" +
    "          ]\n" +
    "        }\n" +
    "      }\n" +
    "    ]\n" +
    "  }\n" +
    "}";


        public static void main (String args []) throws IOException, JSONException
        {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode n1 = objectMapper.readTree(json);
            JsonNode n2 = objectMapper.readTree(jsonCompare);
            System.out.print(n1.equals(n2));

        }


    }
deadpool
  • 314
  • 1
  • 4
  • 10

2 Answers2

2

check Parse a JSON Object With an Undetermined Amount of Child Nodes.

You can get the list of children of one node. Then loop over the children, get the key/value and compare each element of the second node by using the key.

Community
  • 1
  • 1
1

I would suggest use a file comparator and compare line by line. This will work if you also mind the order of the elements in the node. If order is not considered, then first use JsonNode.equals() to check the equality and only if nodes are unequal, use file comparator to print the differences.