question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deep merge for `JsonNode` using `ObjectReader.readTree()`

See original GitHub issue

Is your feature request related to a problem? Please describe.

Describe the solution you’d like Ability to deep merge two JsonNode objects, and/or to deserialize a stream by updating an existing JsonNode rather than creating a new one.

Usage example

    var mapper = new ObjectMapper();
    var node1 = mapper.readTree("{\"root\":{\"a\":\"aaa\",\"foo\":\"hello\"}}");
    var node2 = mapper.readerForUpdating(node1)
        .readTree("{\"root\":{\"b\":\"bbb\",\"foo\":\"goodbye\"}}");

Additional context https://github.com/FasterXML/jackson-databind/issues/584 implies that this capability exists, but it does not appear to be functioning propery for JsonNode. There also doesn’t seem to be a way to specify shallow or deep merge using the tree API readerForUpdating()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
siriannicommented, Apr 16, 2021

Thanks @cowtowncoder for the quick reply! I’ve created a repo https://github.com/sirianni/jsonMerge with a failing unit test.

0reactions
ollijmcommented, Jun 30, 2022

Ah I had a silly mistake, got it now:

        ObjectMapper mapper = new ObjectMapper();
        JsonNode node1 = mapper.readValue("{\"root\":{\"a\":\"aaa\",\"foo\":\"hello\"}}", JsonNode.class);
        JsonNode node2 = mapper.readerForUpdating(node1)
                .readValue("{\"root\":{\"b\":\"bbb\",\"foo\":\"goodbye\"}}");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Merging Two JSON Documents Using Jackson - Stack Overflow
Inspired by StaxMans answer I implemented this merging method. public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) { Iterator<String> ...
Read more >
Jackson JSON - Deep merging with @JsonMerge Annotation
Starting Jackson 2.9 we can use @JsonMerge annotation on object fields to update nested fields with JSON input. In other words now we...
Read more >
Working with Tree Model Nodes in Jackson - Baeldung
This tutorial will focus on working with tree model nodes in Jackson. We'll use JsonNode for various conversions as well as adding, ...
Read more >
ObjectMapper (jackson-databind 2.8.0 API) - FasterXML
Method to deserialize JSON content as tree expressed using set of JsonNode instances. <T extends TreeNode> T, readTree(JsonParser p). Method to deserialize JSON ......
Read more >
Security update for jackson-databind, jackson-dataformats ...
+ Deep merge for 'JsonNode' using 'ObjectReader.readTree()' + IllegalArgumentException: Conflicting setter definitions for property with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found