This is a continuation of question asked in Grouping Json with same value of a key with Jolt
I want to do following changes in output JSON with JOLT. Could you please help on getting the desired JSON output. Thank you.
Input JSON:
[
[
{
"Name": "03.04.2023",
"StartTime": "2023-04-03 06:32",
"SessionData": [
{
"LocationX": "36.282466",
"LocationY": "-5.298164",
"DataValue": "0.36",
"DataType": "distance_1"
},
{
"LocationX": "36.282466",
"LocationY": "-5.298164",
"DataValue": "11.0",
"DataType": "distance_2"
},
{
"LocationX": "36.282466",
"LocationY": "-5.298164",
"DataValue": "2231",
"DataType": "distance_3"
},
{
"LocationX": "36.282466",
"LocationY": "-5.298164",
"DataValue": "0.04",
"DataType": "distance_4"
},
{
"LocationX": "36.282466",
"LocationY": "-5.298164",
"DataValue": "58.82",
"DataType": "distance_5"
},
{
"LocationX": "36.278355",
"LocationY": "-5.290660",
"DataValue": "0.00",
"DataType": "distance_1"
},
{
"LocationX": "36.278355",
"LocationY": "-5.290660",
"DataValue": "9.8",
"DataType": "distance_2"
},
{
"LocationX": "36.278355",
"LocationY": "-5.290660",
"DataValue": "2206",
"DataType": "distance_3"
},
{
"LocationX": "36.278355",
"LocationY": "-5.290660",
"DataValue": "0.00",
"DataType": "distance_4"
},
{
"LocationX": "36.278355",
"LocationY": "-5.290660",
"DataValue": "58.28",
"DataType": "distance_5"
}
]
}
]
]
Expected Output JSON:
[
{
"StartTime": "2023-04-03 06:32",
"distance_1": "0.36",
"LocationX": "36.282466",
"LocationY": "-5.298164",
"distance_2": "11.0",
"X_Z_distance": "2231",
"Y_Z_distance": "0.04",
"Temp_1": 2189,
"Distance_Type": "D",
"Distance_Length": "M"
},
{
"StartTime": "2023-04-03 06:32",
"distance_1": "0.00",
"LocationX": "36.278355",
"LocationY": "-5.290660",
"distance_2": "9.8",
"X_Z_distance": "2206",
"Y_Z_distance": "0.00",
"Temp_1": 2164,
"Distance_Type": "D",
"Distance_Length": "M"
}
]
JOLT:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"Location*": "@(1,LocationX).@(1,LocationY).&",
"@DataValue": "@(1,LocationX).@(1,LocationY).@DataType"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"Temp_1": "=doubleSum(@(1,distance_3),-42)"
}
}
}
},
{
"operation": "default",
"spec": {
"*": {
"*": {
"Distance_Length": "M",
"Distance_Type": "D"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": {
"*": "ONE"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"distance_5": ""
}
}
}
]
You can go on using the previous grouping technique as in the following transformation :
[
{ // you can pull all the expected attributes as some of them are arrays
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"@2,StartTime": "@1,LocationX.@1,LocationY.StartTime",
"Location*": "@1,LocationX.@1,LocationY.&",
"@DataValue": "@1,LocationX.@1,LocationY.@DataType", // match the values of the attributes as a whole
"#D": "@1,LocationX.@1,LocationY.Distance_Type",
"#M": "@1,LocationX.@1,LocationY.Distance_Length"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"distance_5?": null, // prepare this attribute to be removed
"Temp_1": "=intSum(@(1,distance_3),-42)" // expected result attribute is an integer
}
}
}
},
{ // rename distance_3 and _4
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2_&1.&",
"distance_3": "&2_&1.X_Z_distance",
"distance_4": "&2_&1.Y_Z_distance",
"distance_5": { "": "" } // reove this attribute
}
}
}
},
{ // pick only the first elements from the arrays
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
},
{ // derive an array of objects without a key
"operation": "shift",
"spec": {
"*": "[]"
}
}
]