Jolt - Conditional update LHS value - jolt

I basically have 2 different condition which can impact the value of a key in the output.
I have following input:
{
"state": "PRESENT",
"sample": {
"type": "OP"
}
}
I have following spec:
[
{
"operation": "shift",
"spec": {
"state": {
"PRESENT": {
"#ANSWER1": "answer"
},
"*": {
"$": "answer"
}
},
"sample":{
"type":{
"OP":{
"#ANSWER2": "answer"
}
}
}
}
}
]
Now if you see that both the conditions are true, the final output is as following. Both the values come in form of an array
{
"answer" : [ "ANSWER1", "ANSWER2" ]
}
But I am looking for an output based on the second condition. Basically override the existing value in "answer" attribute:
{
"answer" : "ANSWER2"
}

You can add one more shift transformation
{
"operation": "shift",
"spec": {
"*": { "1": { "#": "&2" } }
}
}
to the existing specification in order to pick the second(the element with the index 1) element.
If picking the left element of the array was the case, then cardinality transformation might be preferred to use
{
"operation": "cardinality",
"spec": {
"*": "ONE"
}
}

Related

JoltTransform convert to string

I am strugling a bit with my JoltTransform. I just need to convert the decimal characters to string, but for the life of me I cannot get it to work, as per the example it would be the policy_revision_no and the policy_endorsement_no ..
My example json file I am using is:
{
"policy_unique_reference": "TST1",
"item_unique_reference": "TST2",
"item_parent_item_unique_reference": "",
"item_type": "Cover",
"item_parent_section": "Stuff",
"item_subsection_extension": "",
"policy_revision_no": 1,
"policy_endorsement_no": 2
}
My current Jolt Transform is:
[
{
"operation": "shift",
"spec": {
"*": "Items.&"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"Items": {
"policy_revision_no": "=toString"
}
}
}
}
]
My current result that I am getting is:
{
"Items" : {
"policy_unique_reference" : "TST1",
"item_unique_reference" : "TST2",
"item_parent_item_unique_reference" : "",
"item_type" : "Cover",
"item_parent_section" : "Stuff",
"item_subsection_extension" : "",
"policy_revision_no" : 1,
"policy_endorsement_no" : 2,
"Items" : { }
}
}
Thank you.
You can directly apply modify transform such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"policy_*": "=toString"
}
}
]
presumingly you need to apply for the elements with keys starting with policy_

Jolt Transformation to match on multiple key values

I'm struggling to find the correct jolt spec to evaluate the values of two keys prior to creating an output. Any ideas?
Input:
{
"ticketInformation": {
"area": "001",
"site": "ABC",
"ticketType": "TC"
}
}
Spec:
[
{
"operation": "shift",
"spec": {
"ticketInformation": {
"area": {
"001": {
"#Works": "OneMatch"
}
},
"ticketType": {
"TC": {
"#OnlyEvaluatingTicketType": "HowToEvalueValuesOfTwoKeys"
}
}
}
}
}
]
Output:
{
"OneMatch" : "Works",
"HowToEvalueValuesOfTwoKeys" : "OnlyEvaluatingTicketType"
}
The desired output is the same as the actual output but ONLY if area=001 AND troubleType=TC.
I guess this is the spec you are looking for
[
{
"operation": "shift",
"spec": {
"ticketInformation": {
"area": {
"001": {
"#(2,ticketType)": {
"TC": {
"#Yes": "BothMatching"
}
}
}
}
}
}
}
]

Jolt spec file multiple array of element

I am trying to write a jolt transformation for the input below:
Input
[{
"factValues": [
{
uniqeid:"1",
"values":"1234567"
},
{
uniqeid:"1",
"teams":"abcde"
}]
},{
"factValues": [
{
uniqeid:"2",
"values":"6758595"
},
{
uniqeid:"2",
"teams":"medrgeh"
}]
}
]
Excepted Output.
{
"factValues": [{
uniqeid:"1",
"values":'1234567',
"teams":'76599876'
},
{
uniqeid:"2",
"values":'6758595',
"teams":'medrgeh'
}
]
}
Kindly help me to achieve the expected output.json in spec.json. The spec is not transforming as expected output. I want learn how to use attributes inside the string parser
Shifted all the values to the factValues. Then selected the first element from the uniqueid array,
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"uniqeid": "factValues.[&3].uniqeid",
"values": "factValues.[&3].values",
"teams": "factValues.[&3].teams"
}
}
}
}
}, {
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"uniqeid": "=firstElement(#(1,uniqeid))"
}
}
}
}
]

Jolt transformation json to json for two siftr operations

I am trying to map one a value of input json to a hashmap of the output json and also want to save the value to some another key using jolt json transformation
input json:
{
"metadata": "/a=value1/b=value2/c=value3"
}
spec:
[{
"operation": "shift",
"spec": {
"metadata": {
// match exactly sets of key value pairs
"/*/*/*": {
// pull each one off and accumulate them into a temp array
"$(0,1)": "temp[]",
"$(0,2)": "temp[]",
"$(0,3)": "temp[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"temp": {
"*": {
// match each item by ":" into two captures
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]
output
{
"data" : {
"a" : "value1",
"b" : "value2",
"c" : "value3"
}
}
whereas I also want to map the string metadata to originalData
Expected Output:
{
"data" : {
"a" : "value1",
"b" : "value2",
"c" : "value3"
},
"originalData":"/a=value1/b=value2/c=value3"
}
Spec
[
{
"operation": "shift",
"spec": {
"metadata": {
"#": "originalData",
// match exactly sets of key value pairs
"/*/*/*": {
// pull each one off and accumulate them into a temp array
"$(0,1)": "temp[]",
"$(0,2)": "temp[]",
"$(0,3)": "temp[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"originalData": "originalData", // passthru
"temp": {
"*": {
// match each item by ":" into two captures
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]
[
{
"operation": "modify-overwrite-beta",
"spec": {
"metadata": "=split('/',#(1,metadata))"
}
},
{
"operation": "shift",
"spec": {
"metadata": {
"*": {
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]

How will the JOLT spec look to achieve desired result with the following input?

I am having troubles transforming JSON payload into the desired document.
I have following input:
{
"events": [
{
"recipientId": "r0001"
},
{
"recipientId": "r0002"
}
],
"networkResponseTime": 1234
}
Desired output:
{
"events": [
{
"recipientIds": "r0001",
"networkResponseTime": 1234"
},
{
"recipientIds": "r0002",
"networkResponseTime": 1234"
}
]
}
How will the JOLT spec look like for this example?
So far I have smth like this:
[{
"operation": "shift",
"spec": {
"events": {
"*": {
"recipientId": "events[&1].recipientIds"
}
}
}
}]
Spec
[{
"operation": "shift",
"spec": {
"events": {
"*": {
"recipientId": "events[&1].recipientIds",
//
// go back up to the root of the tree, and then
// come back down the key "networkResponseTime", and
// send it's value to "events[&1].networkResponseTime"
"#(2,networkResponseTime)": "events[&1].networkResponseTime"
}
}
}
}]