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": ""
}
}
]
Related
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"
}
}
}
}
}
]
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
I'm struggling with transformation using JOLT.
Input:
{
"records": [
{"counters": "Item1 Item2 Item3 Item4 Item5 Item6",
"values": "V1 V2 V3 V4 V5 V6"},
{"counters": "Item7 Item8 Item9 Item10 Item11",
"values": "V7 V8 V9 V10 V11"},
{"counters": "Item12 Item13",
"values": "V12 V13"},
{"counters": "Item14",
"values": "V14"}
]
}
Desired output:
{
"xItem1" : "V1",
"xItem2" : "V2",
"xItem3" : "V3",
"xItem4" : "V4",
"xItem5" : "V5",
"xItem6" : "V6",
"xItem7" : "V7",
"xItem8" : "V8",
"xItem9" : "V9",
"xItem10" : "V10",
"xItem11" : "V11",
"xItem12" : "V12",
"xItem13" : "V13",
"xItem14" : "V14"
}
I've almost managed it using this jolt (by replacing toUpper step with the one adding desired "x"):
[
{
"operation": "modify-overwrite-beta",
"spec": {
"records": {
"*": {
"counters": "=split(' ',#(1,counters))",
"values": "=split(' ',#(1,values))"
}
}
}
},
{
"operation": "shift",
"spec": {
"records": {
"*": {
"counters": { "*": "counters[]" },
"values": { "*": "values[]" }
}
}
}
},
{ // ...concat() must instead of toUpper...
"operation": "modify-overwrite-beta",
"spec": {
"counters": {
"*": "=toUpper"
}
}
},
{
"operation": "shift",
"spec": {
"counters": {
"*": {
"*": {
"#(3,values[#2])": "&"
}
}
}
}
}
]
but can't get the last step done - trying all the options, but concat returns either "x" or ItemX, but not xItemX...
Thanks
Not sure if this is what you need, but you dont really need to use concat to add an x to the attributes name, just add x to the rule followed by the &:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"records": {
"*": {
"counters": "=split(' ',#(1,counters))",
"values": "=split(' ',#(1,values))"
}
}
}
},
{
"operation": "shift",
"spec": {
"records": {
"*": {
"counters": { "*": "counters[]" },
"values": { "*": "values[]" }
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"counters": {
"*": "=toUpper"
}
}
},
{
"operation": "shift",
"spec": {
"counters": {
"*": {
"*": {
"#(3,values[#2])": "x&"
}
}
}
}
}
]
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": ""
}
}
}
}
]
I'm wondering if it's possible to sort or bring the min value in case of an array of json. I read something about this issue but found nothing.
This is the Input:
{
"intData": [
{
"DATE": "2018",
"NOME": "raf"
},
{
"DATE": "2001",
"NOME": "fabio"
},
{
"DATE": "2002",
"NOME": "fabiola"
}
]
}
I would:
{
"intData": [
{
"DATE": "2001",
"NOME": "fabio"
},
{
"DATE": "2002",
"NOME": "fabiola"
},
{
"DATE": "2018",
"NOME": "raf"
}
]
}
or
{
"DATE": "2001",
"NOME": "fabio"
}
Is it possible?
Ordered Results
The steps are as follows:
Create object with structure: $.DATE.NOME.#
Sort it
Turn it back into an array
[
{
"operation": "shift",
"spec": {
"intData": {
"*": {
"#": "#(1,DATE).#(1,NOME)"
}
}
}
},
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "intData.[]"
}
}
}
}
]
First result
The steps are as follows:
Create object with structure: $.DATE.NOME.#
Sort it
Turn it back into an array
Get first result
[
{
"operation": "shift",
"spec": {
"intData": {
"*": {
"#": "#(1,DATE).#(1,NOME)"
}
}
}
},
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"0": {
"#": ""
}
}
}
]