transform from one list format to key-value format in JOLT - jolt

I have one scenario where I have to transform below json
{ "Authenticators": [
"ALPHA",
"BETA",
"GAMA"
] }
to
{ "availableAuth":"ALPHA,BETA,GAMA" }

Check this spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"Authenticators": "=join(',',#(1,Authenticators))"
}
}, {
"operation": "shift",
"spec": {
"Authenticators": "availableAuth"
}
}
]

Related

Jolt transformation chunk flat array into pairs of two

I am trying to group pairs of two in a flat list, note that the amount of pairs can be variable (e.g. 0, 2, 4, 6 and so on).
See intended input/output.
Input:
{
"coordinates": [
1.1,
5.1,
1.2,
5.3,
1.3,
5.5
]
}
Output:
{
"coordinates": [
[1.1, 5.1],
[1.2, 5.3],
[1.3, 5.5],
]
}
Is this something that can be easily achieved using a Jolt transformation?
Yes, you can achieve it by using successive transformations such as
[
//index each values seperately
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&"
}
}
},
// convert values to string type so as to prevent the issue of truncation of decimal parts of those values
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=toString(#(1,&))"
}
}
},
// exchange key-value pairs
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "&2.#(0)"
}
}
}
},
{
// increment the values by 1 in order to prepare them for modular arithetic logic held in the following steps
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=intSum(#(1,&),1)"
}
}
},
{
// pairs means to have two components, then need to divide the values by 2 along with rounding to the nearest grater integer
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=divideAndRound(0,#(1,&),2)"
}
}
},
{
// exchange key-value pairs back while keeping the name of the object("coordinates")
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "&2.#(0)"
}
}
}
},
{
// dissipate each component of the list to their proper place
"operation": "shift",
"spec": {
"*": {
"*": "&1[]"
}
}
}
]

Convert Multiple List Of Strings to different objects in Jolt

I have multiple list of strings in my request array. I have to iterate each list and take its first elements and form an object as bucketList similarly second elements of each list and form another object. How can be this achieved in jolt?
Request Packet:
{
"recordSet": [
{
"id": "123",
"bucketIdArray": [ "M03", "M02" ],
"bucketNameArray": [ "EmployeeBucket200", "EmployeeBucket100" ],
"bucketUsageArray": [ "500000000", "5000" ],
"postBucketValueArray": [ "5004000", "5000" ]
},
{
"id": "456",
"bucketIdArray": [ "M04" ],
"bucketNameArray": [ "EmployeeBucket300" ],
"bucketUsageArray": [ "500000000" ],
"postBucketValueArray": [ "5004000" ]
}
]
}
Response Expected:
{
"datas": {
"historyDetails": [
{
"historyId": "123",
"bucketlist": [
{
"bucketId": "M03",
"bucketName": "EmployeeBucket200",
"bucketUsage": "500000000",
"postBucketValue": "5004000"
},
{
"bucketId": "M02",
"bucketName": "EmployeeBucket300",
"bucketUsage": "50000",
"postBucketValue": "5004000"
}
]
},
{
"historyId": "456",
"bucketlist": [
{
"bucketId": "M04",
"bucketName": "EmployeeBucket300",
"bucketUsage": "500000000",
"postBucketValue": "5004000"
}
]
}
]
}
}
You can use this shift spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"id": "datas.historyDetails[&1].&",
"*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
}
}
}
}
]
If the Array strings needed to be removed from the key names, then each key-value pair should explicitly be specified for them such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "datas.historyDetails[&1].&",
"bucketIdArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketId" },
"bucketNameArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketName" },
"bucketUsageArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketUsage" },
"postBucketValueArray": { "*": "datas.historyDetails[&2].bucketlist[&].postBucketValue" }
}
}
}
}
]
Another option would be adding three more steps before our original shift spec in order to prevent hardcoding of each keys individually with Array suffix such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"$": "&2.&.key",
"#": "&2.&.val"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"key": "=split('Array',#(1,&))"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"val": "&.[&2].#(1,key[0])"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"id": "datas.historyDetails[&1].&",
"*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
}
}
}
}
]

JOLT to join different properties

Source JSON:
[
{
"metrics": {
"cost_per_app_install": "0.08",
},
"dimensions": {
"stat_time_day": "2021-06-04 00:00:00",
"campaign_id": 170011516
}
},
{
"metrics": {
"cost_per_app_install": "12.3",
},
"dimensions": {
"stat_time_day": "2021-06-04 00:00:00",
"campaign_id": 17013
}
}
]
Expected:
[
{
"cost_per_app_install": "0.08",
"stat_time_day": "2021-06-04 00:00:00",
"campaign_id": 170011516
},
{
"cost_per_app_install": "12.3",
"stat_time_day": "2021-06-04 00:00:00",
"campaign_id": 17013
}
]
JOLT spec:
[
{
"operation": "shift",
"spec": {
"*": {
"metrics": "[&1]",
"dimensions": "[&1]"
}
}
}
]
I want to add data from dimensions and metrics properties as one combined record. But unable with my JOLT config. This config just removes metrics and dimensions namings. Can I do it only with shift operation?
You need one more step to roam the indexes such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": { "*": "[&2].&" }
}
}
}
]
Btw, no need to explicitly list the key-value pairs for the current case, just "*": "[&1]" should be preferred rather than "metrics": "[&1]","dimensions": "[&1]" for the current pairs if it were the case.

Moving one array into json objects defined in another array - JOLT Transformation

I have my input defined as
{
"lineId": "1",
"Collection": {
"services": [
{
"Code": "TB",
"Type": [
"Data"
]
},
{
"Code": "MAGTB",
"Type": [
"Data"
]
}
]
},
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
}
I would like to get my output as
{
"lineId": "1",
"Collection": {
"services": [
{
"Code": "TB",
"Type": [
"Data"
],
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
},
{
"Code": "TB2",
"Type": [
"Data"
],
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
}
]
}
}
Any help would be appreciated.
I am new to JOLT. And I'm having trouble navigating to the second array from inside the first.
Incomplete transformation that I tried:
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": "Collection.services[].&",
"#(3,lineId)": "lineId",
"#(3,promotions)":{
"*":
}
}
}
}
}
]
edit: Tried this now
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": "Collection.services[]",
// "*": "&",
"#(3,lineId)": "lineId",
"#(3,promotions)": {
"*": {
"Id": "Id"
}
}
}
}
}
}
]
I just have to figure out a way to move the Id list inside the objects in services array.
edit2:
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": {
"*": "Collection.services[&1].&",
"#(3,lineId)": "Collection.services[&1].lineId",
"#(3,promotions)": "Collection.services[&1].promotions"
}
}
}
}
}
]
I think this is the spec you're looking for?
[
{
"operation": "shift",
"spec": {
"lineId": "lineId",
"Collection": {
"services": {
"*": {
"#(3,promotions)": "Collection.services[&1].promotions",
"*": "Collection.services[&1].&"
}
}
}
}
}
]

Jolt giving values as root

Here's my input data :
[
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": "08"
},
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": "20"
}
]
Output
[
{
"TEST1": "root",
"TEST2": "xyz",
"TEST3": ["08","20"]
}
]
Expected Output
[
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": ["08","20"]
}
]
Jolt specs
[
{
"operation": "shift",
"spec": {
"": {
"TEST3": "#(1,TEST2).TEST3"
}
}
},
{
"operation": "shift",
"spec": {
"": {
"$1": "[#2].TEST1",
"$": "[#2].TEST2",
"#.TEST3": "[#2].TEST3"
}
}
}
]
Can you please help me in achieving the excpected output
Check this spec
[
//Concat the TEST1 and TEST2 to temp
{
"operation": "modify-default-beta",
"spec": {
"*": {
"temp": "=concat(#(1,TEST1),':',#(1,TEST2))"
}
}
},
//Group the values by shifting TEST3
{
"operation": "shift",
"spec": {
"*": {
"TEST3": "#(1,temp)"
}
}
},
// Assign the keys to the node named key and value to the node nameed value
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].key",
"$": "[#2].value"
}
}
},
//Shift and assign the respective values to the TEST1, TEST2 and TEST3
{
"operation": "shift",
"spec": {
"*": {
"value": {
"*:*": {
"$(0,1)": "[&3].TEST1",
"$(0,2)": "[&3].TEST2"
}
},
"key": "[&1].TEST3"
}
}
}
]