1

I have two +10.000 lines property files with diferent structure and I need to know which keys are in both files.

I tryed using tools as WinMerge and FileMerge with out any luck.

File1:

.
.
.
  "CardShipmentScale.shipmentScaleRegional": "SomeText1",
  "CardShipmentScale.shipmentScaleRegionalExplanation": "SomeText2",
  "CheckboxCard.DAY": "SomeText3",
  "CheckboxCard.MONTH": "SomeText4",
  "SomeOtherKey.SomeOtherSubKey": "SomeOtherText5"
.
.
.

File2:

{ "global" : {
.
.
.
      CardShipmentScale : {
          shipmentScaleRegional : "SomeText1",
          shipmentScaleRegionalExplanation : "SomeText2"
                          },
      SomeOtherKey : {
          SomeOtherSubKey : "SomeOtherText5"
                     },

.
.
.

}

In this example, I would like to have a tool o report that indicates that CardShipmentScale was found in file1 and file2, but CheckboxCard only in file1.

Any ideas?

Facundo Laxalde
  • 305
  • 5
  • 18
  • What is the format of `File1` and `File2`? `File1` does not look like a valid properties file and `File2` like a valid `JSON` file. Is `global` node contains all sub nodes and it is a root in a file? Which language you want to use? – Michał Ziober Nov 02 '19 at 15:02
  • Michal thanks for your commet, File1 and File2 are both valid Json files, in File2 global contain all sub nodes yes. – Facundo Laxalde Nov 02 '19 at 15:09
  • About language, I’m happy with any one – Facundo Laxalde Nov 02 '19 at 15:09

1 Answers1

1

You can start from this answer Flattening a 3 level nested JSON string in java. I created there JsonFlattener which converts JSON file to Map where key is a JSON Path.

Now it should be easy to check every key from first file in second file:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        File jsonFile0 = new File("./resource/test.json").getAbsoluteFile();
        File jsonFile1 = new File("./resource/test1.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        Map<String, JsonNode> map0 = new JsonFlattener(mapper.readTree(jsonFile0)).flatten();
        Map<String, JsonNode> map1 = new JsonFlattener(mapper.readTree(jsonFile1)).flatten();

        for (Map.Entry<String, JsonNode> node : map0.entrySet()) {
            String keyToCheck = "/global" + node.getKey().replace('.', '/');
            if (map1.containsKey(keyToCheck)) {
                System.out.println(node.getKey() + " exists in both files.");
            } else {
                System.out.println(node.getKey() + " exists only in 1 file.");
            }
        }
    }
}

test.json contains:

{
  "CardShipmentScale.shipmentScaleRegional": "SomeText1",
  "CardShipmentScale.shipmentScaleRegionalExplanation": "SomeText2",
  "CheckboxCard.DAY": "SomeText3",
  "CheckboxCard.MONTH": "SomeText4",
  "SomeOtherKey.SomeOtherSubKey": "SomeOtherText5"
}

test1.json contains:

{
  "global": {
    "CardShipmentScale": {
      "shipmentScaleRegional": "SomeText1",
      "shipmentScaleRegionalExplanation": "SomeText2"
    },
    "SomeOtherKey": {
      "SomeOtherSubKey": "SomeOtherText5"
    }
  }
}

For above files app prints:

/CardShipmentScale.shipmentScaleRegional exists in both files.
/CardShipmentScale.shipmentScaleRegionalExplanation exists in both files.
/CheckboxCard.DAY exists only in 1 file.
/CheckboxCard.MONTH exists only in 1 file.
/SomeOtherKey.SomeOtherSubKey exists in both files.
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • 1
    This is working, I just changed two lines: Map map0 = new JsonFlattener(mapper.readTree(jsonFile0).toString()).flattenAsMap(); Map map1 = new JsonFlattener(mapper.readTree(jsonFile1).toString()).flattenAsMap(); thanks a lot – Facundo Laxalde Nov 03 '19 at 12:17