java 使用JOLT将复杂JSON转换为CSV文件

kninwzqo  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(110)

我是新的JOLT,我不知道它很多,我需要JOLT规范来转换我下面的JSON到CSV文件:

{
    "bigRecords": [
        {
        "Records": {
            "Name": "R1",
            "Id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
            "Type": "New",
            "Area": "Eng",
            "createdAt": "11-10-2022 06:10:00",
            "deletedDate": "11-10-2022 06:20:00",
            "deleted": true,
            "deletedBy": "userId",
            "createdBy": "userId"
            
        },
        "ancestors": [
            {
                "Name": "P1",
                "Type": "DELETE",
                "sta": "",
                "some": "",
                "Id": "1"
            }
        ]
    }, {
        "Records": {
            "Name": "R2",
            "Id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
            "Type": "DRAFT",
            "Area": "Eng",
            "createdAt": "11-10-2022 06:10:00",
            "deletedDate": "11-10-2022 06:20:00",
            "deleted": true,
            "deletedBy": "userId",
            "createdBy": "userId"
        },
        "dependentRecords": [
            {
                "Name": "P3",
                "Type": "DELETE",
                "sta": "1P0",
                "some": "1P02",
                "Id": "1P0"
            }
        ]
    }
    ]
}

请帮助我在转换复杂的json到CSV使用JOlT库,或任何其他方式,我可以做到这一点,在Spring Boot ?

blmhpbnm

blmhpbnm1#

你有JsonFlatener,它很容易使用。https://github.com/wnameless/json-flattener

Map<String, Object> flattenJson = JsonFlattener.flattenAsMap(json);

System.out.println(flattenJson);

// {a.B=1,a.c=空,a.d[0]=假,a.d[1]=真,e=f,g=2.3}

gcxthw6b

gcxthw6b2#

库 * Josson * 具有flatten()函数。它还具有用于反转操作的函数unflatten()
https://github.com/octomix/josson

    • 荒漠化**
Josson josson = Josson.fromJsonString(
    "{" +
    "    \"bigRecords\": [" +
    "        {" +
    "        \"Records\": {" +
    "            \"Name\": \"R1\"," +
    "            \"Id\": \"1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01\"," +
    "            \"Type\": \"New\"," +
    "            \"Area\": \"Eng\"," +
    "            \"createdAt\": \"11-10-2022 06:10:00\"," +
    "            \"deletedDate\": \"11-10-2022 06:20:00\"," +
    "            \"deleted\": true," +
    "            \"deletedBy\": \"userId\"," +
    "            \"createdBy\": \"userId\"" +
    "            " +
    "        }," +
    "        \"ancestors\": [" +
    "            {" +
    "                \"Name\": \"P1\"," +
    "                \"Type\": \"DELETE\"," +
    "                \"sta\": \"\"," +
    "                \"some\": \"\"," +
    "                \"Id\": \"1\"" +
    "            }" +
    "        ]" +
    "    }, {" +
    "        \"Records\": {" +
    "            \"Name\": \"R2\"," +
    "            \"Id\": \"1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01\"," +
    "            \"Type\": \"DRAFT\"," +
    "            \"Area\": \"Eng\"," +
    "            \"createdAt\": \"11-10-2022 06:10:00\"," +
    "            \"deletedDate\": \"11-10-2022 06:20:00\"," +
    "            \"deleted\": true," +
    "            \"deletedBy\": \"userId\"," +
    "            \"createdBy\": \"userId\"" +
    "        }," +
    "        \"dependentRecords\": [" +
    "            {" +
    "                \"Name\": \"P3\"," +
    "                \"Type\": \"DELETE\"," +
    "                \"sta\": \"1P0\"," +
    "                \"some\": \"1P02\"," +
    "                \"Id\": \"1P0\"" +
    "            }" +
    "        ]" +
    "    }" +
    "    ]" +
    "}");
    • 样式1**
JsonNode node = josson.getNode("flatten('.','[%d]')");
System.out.println(node.toPrettyString());
  • 产出 *
{
  "bigRecords[0].Records.Name" : "R1",
  "bigRecords[0].Records.Id" : "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
  "bigRecords[0].Records.Type" : "New",
  "bigRecords[0].Records.Area" : "Eng",
  "bigRecords[0].Records.createdAt" : "11-10-2022 06:10:00",
  "bigRecords[0].Records.deletedDate" : "11-10-2022 06:20:00",
  "bigRecords[0].Records.deleted" : true,
  "bigRecords[0].Records.deletedBy" : "userId",
  "bigRecords[0].Records.createdBy" : "userId",
  "bigRecords[0].ancestors[0].Name" : "P1",
  "bigRecords[0].ancestors[0].Type" : "DELETE",
  "bigRecords[0].ancestors[0].sta" : "",
  "bigRecords[0].ancestors[0].some" : "",
  "bigRecords[0].ancestors[0].Id" : "1",
  "bigRecords[1].Records.Name" : "R2",
  "bigRecords[1].Records.Id" : "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
  "bigRecords[1].Records.Type" : "DRAFT",
  "bigRecords[1].Records.Area" : "Eng",
  "bigRecords[1].Records.createdAt" : "11-10-2022 06:10:00",
  "bigRecords[1].Records.deletedDate" : "11-10-2022 06:20:00",
  "bigRecords[1].Records.deleted" : true,
  "bigRecords[1].Records.deletedBy" : "userId",
  "bigRecords[1].Records.createdBy" : "userId",
  "bigRecords[1].dependentRecords[0].Name" : "P3",
  "bigRecords[1].dependentRecords[0].Type" : "DELETE",
  "bigRecords[1].dependentRecords[0].sta" : "1P0",
  "bigRecords[1].dependentRecords[0].some" : "1P02",
  "bigRecords[1].dependentRecords[0].Id" : "1P0"
}
    • 样式2**
JsonNode node = josson.getNode("flatten('_')");
System.out.println(node.toPrettyString());
  • 产出 *
{
  "bigRecords_0_Records_Name" : "R1",
  "bigRecords_0_Records_Id" : "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
  "bigRecords_0_Records_Type" : "New",
  "bigRecords_0_Records_Area" : "Eng",
  "bigRecords_0_Records_createdAt" : "11-10-2022 06:10:00",
  "bigRecords_0_Records_deletedDate" : "11-10-2022 06:20:00",
  "bigRecords_0_Records_deleted" : true,
  "bigRecords_0_Records_deletedBy" : "userId",
  "bigRecords_0_Records_createdBy" : "userId",
  "bigRecords_0_ancestors_0_Name" : "P1",
  "bigRecords_0_ancestors_0_Type" : "DELETE",
  "bigRecords_0_ancestors_0_sta" : "",
  "bigRecords_0_ancestors_0_some" : "",
  "bigRecords_0_ancestors_0_Id" : "1",
  "bigRecords_1_Records_Name" : "R2",
  "bigRecords_1_Records_Id" : "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
  "bigRecords_1_Records_Type" : "DRAFT",
  "bigRecords_1_Records_Area" : "Eng",
  "bigRecords_1_Records_createdAt" : "11-10-2022 06:10:00",
  "bigRecords_1_Records_deletedDate" : "11-10-2022 06:20:00",
  "bigRecords_1_Records_deleted" : true,
  "bigRecords_1_Records_deletedBy" : "userId",
  "bigRecords_1_Records_createdBy" : "userId",
  "bigRecords_1_dependentRecords_0_Name" : "P3",
  "bigRecords_1_dependentRecords_0_Type" : "DELETE",
  "bigRecords_1_dependentRecords_0_sta" : "1P0",
  "bigRecords_1_dependentRecords_0_some" : "1P02",
  "bigRecords_1_dependentRecords_0_Id" : "1P0"
}

相关问题