I am currently a bit stuck, I need to transform JSON using JOLT but for the life of me I cannot get the data / structure to work with me.
I have the following dataset that I need to transform:
{
"policynumber": "12344",
"registrationnumber": "TST001",
"idnumber": "1234567890",
"policyholder": "Tst Me",
"policyfromdate": "2021-10-11",
"policytodate": "2021-10-31",
"ispolicyvalid": "Yes",
"policyproductcode": "TST111",
"policyproductdescription": "New Product",
"item_parent_section": "Risk",
"item_subsection_extension": "Risk2",
"item_original_start_date": "2021-10-11",
"item_status": "Active",
"item_sum_insured": "5000",
"item_description": "Personal Effects",
"item_asset_no": "ALL0002"
}
The Desired outcome should be something like this:
{
"policyNumber": "12344",
"registrationNumber": "TST001",
"idNumber": "1234567890",
"policyholder": "Tst Me",
"policyFromDate": "2021-10-11",
"policyToDate": "2021-10-31",
"isPolicyValid": "Yes",
"policyProductCode": "TST111",
"policyProductDescription": "New Product",
"riskCategories": {
"item_parent_section": "Risk",
"item_subsection_extension": "Risk2",
"riskItemAllRisk": [
{
"item_status": "Active",
"item_description": "Personal Effects",
"item_sum_insured": "5000",
"item_original_start_date": "2021-10-11",
"item_asset_no": "ALL0002"
}
]
}
}
I might have made a mistake in my desired outcome but basically, the "riskItemAllRisk" part needs to be in a array.
My JOLT Transform that I am working on is:
[
{
"operation": "shift",
"spec": {
"policynumber": "policyNumber",
"registrationnumber": "registrationNumber",
"idnumber": "idNumber",
"policyholder": "policyholder",
"policyfromdate": "policyFromDate",
"policytodate": "policyToDate",
"ispolicyvalid": "isPolicyValid",
"policyproductcode": "policyProductCode",
"policyproductdescription": "policyProductDescription"
}
},
{
"operation": "default",
"spec": {
"riskCategories": {
"item_parent_section": "itemparentsection",
"item_subsection_extension": "itemsubsectionextension"
}
}
},
{
"operation": "default",
"spec": {
"riskItemAllRisk[]": {
"0": {
"item_original_start_date": "2021-10-11",
"item_status": "item_status",
"item_sum_insured": "item_sum_insured",
"item_description": "item_description",
"item_asset_no": "item_asset_no"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"idNumber": "=toString"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"itemsuminsured": "=toString"
}
}
]
Apologies for the horrible code, but this is a first for me.
Thank you.
CodePudding user response:
One shift transformation would be enough such as
[
{
"operation": "shift",
"spec": {
"policynumber": "&",
"registrationnumber": "&",
"idnumber": "&",
"policyholder": "&",
"policyfromdate": "&",
"policytodate": "&",
"ispolicyvalid": "&",
"policyproductcode": "&",
"policyproductdescription": "&",
"item_parent_section": "riskCategories.&",
"item_subsection_extension": "riskCategories.&",
"*": "riskCategories.riskItemAllRisk[0].&"
}
}
]
where the values of item_parent_section
and item_subsection_extension
attributes are qualified with riskCategories
first and the remaining elements of the array by riskCategories.riskItemAllRisk[0]