javaでjson文字列を操作する方法

以下のような二つのjsonを1つのjsonにマージする

``` target1.json
{
    "detail": [
        {
            "itemId": "a1",
            "itemName": "a1_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a2",
            "itemName": "a2_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a3",
            "itemName": "a3_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a4",
            "itemName": "a4_a",
            "lastuUpdate": ""
        }
    ]
}
```

```target2.json
{
    "detail": [
        {
            "itemId": "b1",
            "itemName": "b1_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b2",
            "itemName": "b2_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b3",
            "itemName": "b3_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b4",
            "itemName": "b4_a",
            "lastuUpdate": ""
        }
    ]
}
```


``` App.java

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class App {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();

        // JSONを文字列として定義
        String jsonString1 = "{"
                + "\"detail\": ["
                + "    {\"itemId\": \"a1\", \"itemName\": \"a1_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"a2\", \"itemName\": \"a2_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"a3\", \"itemName\": \"a3_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"a4\", \"itemName\": \"a4_a\", \"lastuUpdate\": \"\"}"
                + "]"
                + "}";

        String jsonString2 = "{"
                + "\"detail\": ["
                + "    {\"itemId\": \"b1\", \"itemName\": \"b1_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"b2\", \"itemName\": \"b2_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"b3\", \"itemName\": \"b3_a\", \"lastuUpdate\": \"\"},"
                + "    {\"itemId\": \"b4\", \"itemName\": \"b4_a\", \"lastuUpdate\": \"\"}"
                + "]"
                + "}";

        try {
            // 文字列からJSONノードを解析
            JsonNode json1 = mapper.readTree(jsonString1);
            JsonNode json2 = mapper.readTree(jsonString2);

            // "detail"配列を取得
            ArrayNode details1 = (ArrayNode) json1.get("detail");
            ArrayNode details2 = (ArrayNode) json2.get("detail");

            // 2つの配列をマージ
            details1.addAll(details2);

            // マージした結果を新しいJsonNodeに格納
            JsonNode mergedJson = mapper.createObjectNode().set("detail", details1);

            // 結果を標準出力に表示
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mergedJson));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

``` 出力値.json
{
    "detail": [
        {
            "itemId": "a1",
            "itemName": "a1_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a2",
            "itemName": "a2_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a3",
            "itemName": "a3_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "a4",
            "itemName": "a4_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b1",
            "itemName": "b1_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b2",
            "itemName": "b2_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b3",
            "itemName": "b3_a",
            "lastuUpdate": ""
        },
        {
            "itemId": "b4",
            "itemName": "b4_a",
            "lastuUpdate": ""
        }
    ]
}
```