Re-arrange field in JOLT - json

I am trying to reposition a field called "distributedAmount" in my JOLT output. I want "distributedAmount" after "valid rules" tag.
What I tried-
But the above solution didn't worked. Any idea on what changes I need to make in my spec. d Desired Output-
Input is to long so I have uploaded the input and the jolt spec in Google Drive. Below is the link- https://drive.google.com/drive/folders/1vvdFBPwaHRVvjttUTQP0jzQlqYlGfjFZ?usp=share_link

You can convert the line 402 to
"#(2,distributedAmount)": "&9.&8.&7.&6.&5.&4.&3.&2.&1.zdistributedAmount"
by prepending distributedAmount on the right hand side with a z, and then add the following specs to the current one
{
"operation": "sort"
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"distributedAmount": "#(1,zdistributedAmount)"
}
}
}
}
}
}
}
}
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"zdistributedAmount": ""
}
}
}
}
}
}
}
}
}
}
}
}
where sorting is handled in a bit tricky way by prepending a z to the distributedAmount key, and renamed back to the original.

Related

Masking cards usgins jolt

How can I iterate through list and replace some elements in it? I have this input
{
"clients": [
{
"clientId": "166734",
"info": {
"cards": [
"378282246310005",
"371449635398431"
]
}
}
]
}
I want mask cards like this "cards" : [ "3782*****0005", "3714*****8431" ]
You can use shift transformations to convert the cards array to indexed and 0,1,** labeled objects in order to prepare for modify transformation spec in which substring and concat functions are used such as
[
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": {
"#": "&5[#5].&3.&2.&1.&"
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": {
"p1": "=substring(#(1,&1),0,4)",
"p2": "=substring(#(1,&1),11,15)",
"cs": "=concat(#(1,p1),'********',#(1,p2))"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": {
"cs": "&5[#5].&3.&2[]"
}
}
}
}
}
}
}
]

Jolt Spec for masking cards

How can I iterate through list and replace some elements in it?
I have this input
{
"clients": [
{
"clientId": "166734",
"info": {
"cards": [
"378282246310005",
"371449635398431"
]
}
}
]
}
What I expect cards will looks like this "cards" : [ "3782", "3714" ]
But in my spec do not work substring
[
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": "=substring(#(1,&),0,#(1,4))"
}
}
}
}
}
}
]
Note 1:
You should not use #(1,&) for getting an array index.
#(1,&) says: Go up 1 level (cards) and get 0 and 1 key from the array with &. But You have an array and should get the index from it.
You can say get & in an array like this: #(1,[&])
Note 2:
For getting the first 4 elements in the string you don't need the #(1,4). just say 4
Simpler solution:
We can get the current level without going up 1 level with #(0) or #0: #0,0,4
[
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": "=substring(#0,0,4)"
}
}
}
}
}
}
]
You can use shift transformations to tame the cards array in order to prepare for modify transformation spec such as
[
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": {
"#": "&5[#5].&3.&2.&"
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": "=substring(#(1,&),0,4)"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": "&4[#4].&2.&1[#1]"
}
}
}
}
}
}
]
If there can always only exist two components for cards then we make the whole spec shorter by using [first/last]Element functions, otherwise use "=(#(1,cards[0/1]))" by individually writing such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"c0": "=firstElement(#(1,cards))", //or "=(#(1,cards[0]))"
"c1": "=lastElement(#(1,cards))", //or "=(#(1,cards[1]))"
"cards0": "=substring(#(1,c0),0,4)",
"cards1": "=substring(#(1,c1),0,4)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"*s*": "&3[#3].&1.&(0,1)s"
}
}
}
}
}
]

JOLT transformation which involve linking by an attibutes value

I need to transform the following input JSON into the format outlined here below using JOLT. The linkage is to be done by the deps.name of first array element with spec.name of the second array element. I am clueless about the linking. Thanks for your help.
Input json
[
{
"key": "Primary",
"metadata": {
"name": "35f8d9fac891"
},
"deps": [
{
"name": "e6ae6d29edf8"
}
],
"spec": {
"vattr1": "vval1",
"vattr2": "vval2"
}
},
{
"key": "Secondary",
"metadata": {
"name": "hp74z"
},
"spec": {
"name": "e6ae6d29edf8",
"nattr1": "nval1",
"nattr2": "nval2",
"deps": {
"Name": "5505da219463"
}
}
}
]
Output format expected
{
"key": "Primary",
"metadata": {
"name": "35f8d9fac891"
},
"deps": [
{
"name": {
"key": "Secondary",
"metadata": {
"name": "hp74z"
},
"spec": {
"name": "e6ae6d29edf8",
"nattr1": "nval1",
"nattr2": "nval2",
"deps": {
"Name": "5505da219463"
}
}
}
}
],
"spec": {
"vattr1": "vval1",
"vattr2": "vval2"
}
}
Start with this spec in order to see whether those two objects of the array to be accumulated within a new array with key a common name("e6ae6d29edf8") such as
{
"operation": "shift",
"spec": {
"*": {
"#(0)": "#(1,spec.name)",
"#": "#(1,deps[&].name)"
}
}
}
the test would fail, if those names(one from spec.name and the other from deps[].name) don't match(just try it!)
then use
{
"operation": "shift",
"spec": {
"*": {
"*": "common"
}
}
}
in order to get rid of that confusing key name("e6ae6d29edf8") by converting it to "common". Now, we get array of two objects, and need to replace the value of "deps" array with the second object. modify transformation spec is designed to do such manipulations. Then, pick only the "common" object by using a shift transformation spec.
Thus, full spec will be :
[
{
"operation": "shift",
"spec": {
"*": {
"#(0)": "#(1,spec.name)",
"#": "#(1,deps[&].name)"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "common"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=firstElement(#(1,&))",
"SecondaryObject": "=lastElement(#(1,common))"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"common": {
"deps": {
"*": "=(#(3,SecondaryObject))"
}
}
}
},
{
"operation": "shift",
"spec": {
"common": {
"*": "&"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Edit(considering the case that there exists multiple objects to be embedded as commented) :
Then you can determine an attribute "name_val" in the first spec to be used as the common identifier value to be used within the upcoming specs such as
[
{
"operation": "shift",
"spec": {
"*": {
"#(0)": "#(1,spec.name)",
"#": "#(1,deps[&].name)",
"#(0,deps[&].name)": "name_val"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#(2,name_val)": {
"#1": "common[&2]"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"common": {
"0": "c1",
"*": "c2"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"c1": {
"deps": {
"*": {
"*": "=(#(4,c2))"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"c1": ""
}
}
]

With JOLT need to Transform JSON object with If Then Else Condition

Below is my Input Json
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"DE35": "123654ABC54678",
"DE45": "898454PQR54678"
}
}
]
I need to Trnsaform this json in such a way if DE35 has value say of substring 6-9 then take DE35 value in Transform Json, if DE35 do not have value then Take DE45 value in transform Json.
So output will be if DE35 Available then
[ {
"COR_ID" : "12345",
"payloadFormat" : "Money",
"EXT_SERV_CD":"ABC"
} ]
if DE35 is not available then the output should be
[ {
"COR_ID" : "12345",
"payloadFormat" : "Money",
"EXT_SERV_CD":"PQR"
} ]
I am using Below JOLT spec to do Transformation but it is not working.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "&",
"payload": {
"DE|DE35": "&2.payload.TMPDE35"
}
}
}
}, {
"operation": "modify-default-beta",
"spec": {
"*": {
"payload": {
"DE35Val": "=substring(#(1,TMPDE35), 6, 9)"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "&",
"payload": {
"DE|DE45": "&2.payload.TMPDE45"
}
}
}
}, {
"operation": "modify-default-beta",
"spec": {
"*": {
"payload": {
"DE45Val": "=substring(#(1,TMPDE45), 6, 9)"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"correlationId": "[&1].COR_ID",
"payloadFormat": "[&1].payloadFormat",
"payload": {
"DE35Val": {
"ABC": {
"DE35Val": "[#3].payload.EXT_SERV_CD"
},
"false": {
"DE45Val": "[#3].payload.EXT_SERV_CD"
}
}
}
}
}
}
]
Please suggest where I am making mistake.
This spec should work for you
[
{
"operation": "modify-default-beta",
"spec": {
"*": {
"TmpDE35": "=substring(#(1,payload.DE35), 6, 9)",
"TmpDE45": "=substring(#(1,payload.DE45), 6, 9)"
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"Tmp35_45": "=concat(#(1,TmpDE35), #(1,TmpDE45))"
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"EXT_SERV_CD": "=substring(#(1,Tmp35_45), 0, 3)"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"Tmp*": null,
"payload": null
}
}
}
]
The solution idea:
Move the substrings of the DE35, DE45 fields to the TmpDE35, TmpDE45
Concatenate TmpDE35, TMPDE45 and store it in the Tmp35_45. If TmpE35 is empty then the first 3 characters will come from TmpDE45
Get the substring Tmp35_45
Clean up, shift

JOLT Transform - pattern matching

I am trying to transform input fields by matching pattern like "*|*|*" where the words are separated by |. However, I am not able to get it working. If I replace | by :, the spec works. Is there a special meaning to |, how can use this as part of regular expressions?
Input :
{
"items": [
{
"itemId": "1234500AA",
"additionalData": "12345|20001|3000"
}
]
}
Spec :
[
{
"operation": "shift",
"spec": {
"items": {
"*": {
"itemId": "ID",
"additionalData": {
"*|*|*": {
"$(0,1)": ["cartMenuItems[&3].baseProductId", "cartMenuItems[&3].mdmId"],
"$(0,2)": "cartMenuItems[&3].code",
"$(0,3)": "cartMenuItems[&3].sku"
}
}
}
}
}
}
]
Output Expected :
{
"ID": "1234500AA",
"cartMenuItems": [
{
"baseProductId": "12345",
"mdmId": "12345",
"code": "20001",
"sku": "3000"
}
]
}
However, I get an error like below:
JOLT Chainr encountered an exception constructing Transform
className:com.bazaarvoice.jolt.Shiftr at index:0.
Jolt doesn't seem to like | (as you would need to escape them), you could replace the | :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"items": {
"*": {
"additionalDataTemp": "=split('\\|',#(1,additionalData))",
"additionalData": "=concat(#(1,additionalDataTemp[0]),'_',#(1,additionalDataTemp[1]),'_',#(1,additionalDataTemp[2]))"
}
}
}
},
{
"operation": "shift",
"spec": {
"items": {
"*": {
"itemId": "ID",
"additionalData": {
"*_*_*": {
"$(0,1)": ["cartMenuItems[&3].baseProductId", "cartMenuItems[&3].mdmId"],
"$(0,2)": "cartMenuItems[&3].code",
"$(0,3)": "cartMenuItems[&3].sku"
}
}
}
}
}
}
]
Or another way of doing it would be:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"items": {
"*": {
"additionalData": "=split('\\|', #(1,additionalData))",
"baseProductId": "#(1,additionalData[0])",
"mdmId": "#(1,additionalData[0])",
"code": "#(1,additionalData[1])",
"sku": "#(1,additionalData[2])"
}
}
}
},
{
"operation": "shift",
"spec": {
"items": {
"*": {
"itemId": "ID",
"*": "cartMenuItems[0].&"
}
}
}
},
{
"operation": "remove",
"spec": {
"cartMenuItems": {
"*": {
"additionalData": ""
}
}
}
}
]