-1

I have two JsonNode objects, A and B, which both contain an identical field, "segments". I need to take the content from JsonNode object A and append it to the content from B. I need both the values from A and B in the final object.

My code right now takes the right content from A, but it replaces everything from B. Can anyone help me with a solution? In the end, I need to obtain an object like C, as you can see below.

public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) {

        JsonNode segmentedConfigs = updateNode.get("segments");
        ((ObjectNode) mainNode).put("segments", segmentedConfigs);
        return mainNode;
    }

JsonNode A:

{
  "segments": {
    "A": {},
    "B": {},
    "C": {}
  },
  "enabled": false,
  "configStickyByClub": false,
  "sinks": {}
}

JsonNode B:

{
  "segments": {
    "D": {},
    "E": {},
    "F": {}
  },
  "enabled": false,
  "configStickyByClub": false,
  "sinks": {}
}

JsonNode C:

{
  "segments": {
    "A": {},
    "B": {},
    "C": {},
    "D": {},
    "E": {},
    "F": {}
  },
  "enabled": false,
  "configStickyByClub": false,
  "sinks": {}
}
Maria1995
  • 439
  • 1
  • 5
  • 21

1 Answers1

0

You need to recursively merge the child nodes if they are both ObjectNodes:

public static void deepMerge(ObjectNode lhsNode, ObjectNode rhsNode) {
    rhsNode.fields().forEachRemaining(entry -> {
        final String key = entry.getKey();
        final JsonNode rhsChildNode = entry.getValue();
        if (rhsChildNode.isObject()) {
            final JsonNode lhsChildNode = lhsNode.get(key);
            if (lhsChildNode != null && lhsChildNode.isObject()) {
                deepMerge((ObjectNode)lhsChildNode, (ObjectNode)rhsChildNode);
            } else {
                lhsNode.put(key, rhsChildNode);
            }
        } else {
            lhsNode.put(key, rhsChildNode);
        }
    });
}

private static final String jsonA = "{\n" +
        "  \"segments\": {\n" +
        "    \"A\": {},\n" +
        "    \"B\": {},\n" +
        "    \"C\": {}\n" +
        "  },\n" +
        "  \"enabled\": false,\n" +
        "  \"configStickyByClub\": false,\n" +
        "  \"sinks\": {}\n" +
        "}";

private static final String jsonB = "{\n" +
        "  \"segments\": {\n" +
        "    \"D\": {},\n" +
        "    \"E\": {},\n" +
        "    \"F\": {}\n" +
        "  },\n" +
        "  \"enabled\": false,\n" +
        "  \"configStickyByClub\": false,\n" +
        "  \"sinks\": {}\n" +
        "}";

public static void main(String[] args) throws IOException {

    final ObjectMapper om = new ObjectMapper();

    final ObjectNode nodeA = (ObjectNode)om.readTree(new StringReader(jsonA));
    final ObjectNode nodeB = (ObjectNode)om.readTree(new StringReader(jsonB));

    deepMerge(nodeA, nodeB);

    System.out.println(nodeA.toPrettyString());
}

Output:

{
  "segments" : {
    "A" : { },
    "B" : { },
    "C" : { },
    "D" : { },
    "E" : { },
    "F" : { }
  },
  "enabled" : false,
  "configStickyByClub" : false,
  "sinks" : { }
}
jon hanson
  • 8,722
  • 2
  • 37
  • 61