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.
- Add "StartTime" for each object in output json
- Rename istance_3 and distance_4 into X_Z_distance and Y_Z_distance
- Round Temp_1 value into 2 decimal points
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": ""
}
}
}
]
2
Answers
Your initial JOLT specification didn’t properly reference the StartTime field at the correct level. You were attempting to access it at level *, but it should have been at level "0". did not include renaming distance_3 and distance_4 to X_Z_distance and Y_Z_distance respectively.
Incorrect Modification of Temp_1 and your JOLT spec lacked setting default values for Distance-length and distance-type.
also I removed the distance_5, your JOLT spec didn’t include the removal of the distance_5 key as specified in the desired output.
here is the edited JOLT;
You can go on using the previous grouping technique as in the following transformation :