JOLT to join different properties - jolt

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.

Related

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

Map Jolt Transformation

I am trying to transform the following input:
{
"test_types": {
"3": "Type3",
"21": "type21",
"16": "Type16",
"15": "type15"
},
"name": "BlackSquid",
"obj_name": "malware",
"value": "2341743",
"type": "16"
}
Into this output:
{
"test_types": {
"3": "Type3",
"21": "type21",
"16": "Type16",
"15": "type15"
},
"name": "BlackSquid",
"obj_name": "malware",
"value": "2341743",
"type": "16",
"type_name": "Type16"
}
I have tried to use a "modify-overwrite-beta" spec by hardcoding the key value which gives me the result I need:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"type_name": "#(1,test_types.16)"
}
}
]
but I would like to use the "type" value dynamically. The following throws an exception but maybe something like this:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"type_name": "#(1,test_types.#(1,type))"
}
}
]
No need to use modify-overwrite-beta spec, but condition based shift spec for the key type would suffice such as
[{
"operation": "shift",
"spec": {
"*": "&",
"type": {
"*": {
"#(2,type)": "type",
"#(2,test_types.&)": "type_name"
}
}
}
}]

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].&"
}
}
}
}
}
]

transform from one list format to key-value format in 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"
}
}
]

Add array inside object with same key in jolt spec

I would like to move the recommendations array by row_id key inside the investors with the same row_id
Original Json
{
"investors": [
{
"row_id": 1,
"name": "AAAA"
},
{
"row_id": 2,
"name": "BBBB"
}
],
"recommendations": [
{
"row_id": "1",
"title": "ABC"
},
{
"row_id": "2",
"title": "CDE"
}
]
}
I've tried a lot of specs at https://jolt-demo.appspot.com with no success
Specs tried...
[{
"operation": "shift",
"spec": {
"investors": {
"*": "investors[]"
},
"recommendations": {
"#": "recommendations[]"
}
}
}]
Desired Json
{
"investors": [
{
"row_id": 1,
"name": "AAAA",
"recommendations":[{
"row_id": "1",
"title": "ABC"
}]
},
{
"row_id": 2,
"name": "BBBB",
"recommendations":[{
"row_id": "2",
"title": "CDE"
}]
}
]
}
This can be done in two stage shift
First shift groups everything based on row_id.
(I'd suggest running the first shift of its own to see what the output is)
Second shift uses that grouped output and formats results.
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"row_id": {
"*": {
"#2": "&.&4"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"investors": "investors.[#2]",
"recommendations": "investors.[#2].recommendations[]"
}
}
}
]