Home > OS >  Convert Complex JSON To Flat JSON using JOLT
Convert Complex JSON To Flat JSON using JOLT

Time:01-18

I am new to JOLT and I don't know much about it, and I need the JOLT spec to convert my below JSON to Flat Json:

{
    "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"
            }
        ]
    }
    ]
}

Please help me in convert the complex json to flaten json using the JOlT library, or any other way I can do achieve this, in spring boot?

CodePudding user response:

You have JsonFlattener which is easy to use. https://github.com/wnameless/json-flattener

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

System.out.println(flattenJson);

// {a.b=1, a.c=null, a.d[0]=false, a.d[1]=true, e=f, g=2.3}

CodePudding user response:

Library Josson has flatten() function. It also has function unflatten() to reverse the operation.

https://github.com/octomix/josson

Deserialization

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\""  
    "            }"  
    "        ]"  
    "    }"  
    "    ]"  
    "}");

Style 1

JsonNode node = josson.getNode("flatten('.','[%d]')");
System.out.println(node.toPrettyString());

Output

{
  "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"
}

Style 2

JsonNode node = josson.getNode("flatten('_')");
System.out.println(node.toPrettyString());

Output

{
  "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"
}
  • Related