Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectMapper readerForUpdating to replace array

Tags:

java

jackson

I have two different JSON files.

File A:

{
    "label": "A",
    "links": [
        {
            "url": "urla"
        }
    ]
}

File B:

{
    "links": [
        {
            "url": "urlb"
        }
    ]
}

Now I want to update A with the contents of B to get the following result JSON:

{
    "label": "A",
    "links": [
        {
            "url": "urlb"
        }
    ]
}

That is the links array should be fully replaced with the contents of B. But instead it merges the two Arrays:

{
    "label": "A",
    "links": [
        {
            "url": "urla",
            "url": "urlb"
        }
    ]
}

This is not desired. The code for the merged file:

JsonNode A = ... // resolved from a service call
JsonNode B = ... // resolved from a service call
ObjectMapper mapper = new ObjectMapper();
result = mapper.readerForUpdating(A).readValue(B);

I also tried to set mapper.setDefaultMergeable(false); but it didn't help.

I use com.fasterxml.jackson.core:jackson-core:jar:2.9.8

Can someone help me?

like image 752
Ralf Avatar asked May 03 '26 20:05

Ralf


1 Answers

I think this can help: mapper.configOverride(ArrayNode.class).setMergeable(false)

like image 121
Yakov Burtsev Avatar answered May 05 '26 10:05

Yakov Burtsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!