Jolt Transform: Single Value can't be added to an Array - json

I have the following Jolt Spec
[
{
"operation": "shift",
"spec": {
"*": {
"value": "[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": "values"
}
},
{
"operation": "default",
"spec": {
"values": []
}
}
]
For the following Input:
[
{
"value": 175
},
{
"value": 160
}
]
I get the expected result as following:
{
"values" : [ 175, 160 ]
}
And for the following Input:
[
{
"valueNum": 175
}
]
I again get an expected result as follows:
{
"values" : [ ]
}
But for the following input :
[
{
"value": 175
}
]
I get the following output
{
"values" : 175
}
I want to have the values in an array even if there is just one element in it like below:
{
"values" : [175]
}
Could you please help me fixing my Jolt Spec to get the desired result? Thanks!

Just using a single shift transformation spec as the following one would suffice
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&s[]"
}
}
}
]
where & is a substitution for the key name(value), and s is a suffix to make the word plural
the demoes on the site http://jolt-demo.appspot.com/ are
Edit : If you need an output based on seperating the keys of the attributes to be value or the others(such as valueNum), then still a single spec like the one below would be sufficient :
[
{
"operation": "shift",
"spec": {
"*": {
"value": "&s[]",
"*": "&[]"
}
}
}
]

With the following spec, I was able to achieve all my use cases
[
{
"operation": "shift",
"spec": {
"*": {
"value": "[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": "values[]"
}
},
{
"operation": "default",
"spec": {
"values": []
}
}
]

Related

How to split the nested JSON in Jolt specification

pls help me out wit this jolt specification. pls help me
Notes :
Resourcename is the last element of ResourceId which will be a new attribute that we need to add to the expected output
Tags field needs to be copied and splited as mentioned in the expected output.
Input :
[
{
"ResourceId": "/subscriptions/bb842437aa4/resourceGroups/ECHLABHENKEL/providers/Microsoft.Compute/virtualMachines/pmoapps",
"Tags": "Name\": \"PMOapplication\",\"Owner\": \"Breil sathish"
},
{
"ResourceId": "/subscriptions/bb842437aa4/resourceGroups/HCLTECHLABHENKEL/providers/Microsoft.Compute/virtualMachines/pmoapps",
"Tags": "Name\": \"PMOapplication\",\"Owner\": \"Breil sathish1"
}
]
Expected Output :
[
{
"ResourceId": "/subscriptions/bb842437aa4/resourceGroups/ECHLABHENKEL/providers/Microsoft.Compute/virtualMachines/pmoapps",
"Tags": "Name\": \"PMOapplication\",\"Owner\": \"Breil sathish",
"Resourcename": "pmoapps",
"Name": "PMOapplication",
"Owner": "Breil sathish"
},
{
"ResourceId": "/subscriptions/bb842437aa4/resourceGroups/HCLTECHLABHENKEL/providers/Microsoft.Compute/virtualMachines/pmoapps",
"Tags": "Name\": \"PMOapplication\",\"Owner\": \"Breil sathish1",
"Resourcename": "pmoapps",
"Name": "PMOapplication",
"Owner": "Breil sathish1"
}
]
Thanks,
N Sathish
You can use the following transformation spec as reading the explanations stated at the beginning of each of them such as
[
{ // Convert the value of the attributes to individual arrays with the identical names
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"ResourceName": "=split('/', #(1,ResourceId))",
"tag": "=split(',', #(1,Tags))"
}
}
},
{ // Generate tag0 and tag1 attributes by splitting members of the "tag" array
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"tag": {
"*": {
"#": "&3.&2&1"
}
}
}
}
},
{ // Split related strings by colon characters for "tag" array while deriving the last element of "ResourceName" array
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"R*": "=lastElement(#(1,&))",
"tag*": "=split(': ', #(1,&))"
}
}
},
{ // Match components of "tag" array component 1 against component 2
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"tag*": {
"#1,&[1]": "&2.&1.#(2,&[0])"
}
}
}
},
{ // Split the values by \" character combination
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"tag*": {
"*": "=split('\"', #(1,&))"
}
}
}
},
{ // Prune undesired values for right-hand-side
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"tag*": {
"*": "=join('', #(1,&))"
}
}
}
},
{ // Prune undesired values for left-hand-side(keys)
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"tag*": {
"\"*\"": "[&2].&(0,1)",
"*\"": "[&2].&(0,1)"
}
}
}
}
]

JOLT Specification to Transform JSON field value to field name

I am trying to convert the below JSON payload into a JSON which has the field name as the value of the field:
The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.
Can you please help to provide jolt specifications for this?
Input JSON Payload :
{
"action": {
"allowPartialSuccess": true,
"records": [
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
},
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
}
],
"type": "update"
}
}
`
Expected output JSON Payload :
{
"action" : {
"allowPartialSuccess" : true,
"records" : {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
}, {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
} ,
"type" : "update"
}
}
JOLT specification that I am using :
[
{
"operation": "shift",
"spec": {
"action": {
"allowPartialSuccess": "action.allowPartialSuccess",
"records": {
"*": {
"CPQSubscriptionID__c": "action.records.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
},
"type": "action.type"
}
}
}
]
You can use this shift transformation spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&", // &1 replicates the literal "action"
"records": {
"*": {
"C*": "&3.&2.#(1,ZuoraSubscriptionExtId).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Your expected output is wrong in the question.
If you want an object in your record, You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"&": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"records": {
"*": {
"*": "ONE"
}
}
}
}
}
]
And if you want an array in your output. You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2[].#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
}
]

JOLT JSON transform values from one-to-many to one-to-one

I'm trying to map one key to each value in array to a new array by using JOLT.
Could someone please help give me a solution for this:
My JSON:
[
{
"person_id": "1",
"resources": ["asd", "zxc"]
},
{
"person_id": "2",
"resources": ["ghj", "asd"]
}
]
And my expected JSON:
[
{
"person_id": "1",
"resource": "asd"
},
{
"person_id": "1",
"resource": "zxc"
},
{
"person_id": "2",
"resource": "ghj"
},
{
"person_id": "2",
"resource": "asd"
}
]
I had tried this Jolt Specification
[
{
"operation": "shift",
"spec": {
"*": {
"resources": {
"*": {
"#(2,person_id)": "[&].person_id",
"#": "[&].resource"
}
}
}
}
}
]
But no luck it always maps all values at the same index to 1 array.
You can use two consecutive shift transformation specs by walking through the resources array within the first one such that
[
{
"operation": "shift",
"spec": {
"*": {
"*s": { // this tecnique stands for extracting by the replacement of the asterisk through use of &(2,1) below
"*": {
"#(2,person_id)": "[&3].&1.person_id", // to separate by indexes of the array to generate three level for three independent objects
"#": "[&3].&1.&(2,1)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
You can use this jolt spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*s": {
"*": {
"#(2,person_id)": "#(1).person_id",
"#": "#(1).&(2,1)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]

Truncate every string in an array with JOLT Transformation

I try to truncate strings in an array with JOLT. The character "^" should be removed from any string that contains it in the first position.
Sample Input:
{
"scores": [
"^aaaa",
"^bbbb",
"cccc",
"^dddd",
"eeee"
]
}
Expected Output:
{
"scores" : [ "aaaa", "bbbb", "cccc", "dddd", "eeee" ]
}
My Spec:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"splittedScores": "=split('\\^',#(1,scores))",
"scores": "=join('',#(1,splittedScores))" // <--- Not working
}
}
]
Seems you'd need one extra shift transformation sandwiched between modify transformations to tame the subarrays such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=split('\\^',#(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "&"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join('',#(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "scores"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
or only one shift transformation will be sufficient to use(a straightforward option) such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"^*": {
"$(0,1)": "&3"
},
"*": {
"$": "&3"
}
}
}
}
}
]
splitting by "$(0,1)" wildcard for the object under "^*" node. &3 represents going three levels up to grab the name of the key scores .
the demo on the site http://jolt-demo.appspot.com/ is
If ^ is being removed in the first position really matters, then the second method; if all ^ characters from any strings should be removed, then the first method should be used.

How to compare filed values in JOLT transformation?

I have the input like below,
Input:
{
"a": 1,
"b": 2
}
Spec:
[
{
"operation": "shift",
"spec": {
"a": {
"#(2,b)": {
"#Matched": "result"
},
"*": {
"#Not Matched": "result"
}
}
}
}]
Output:
{
"result" : [ "Matched", "Not Matched" ]
}
Expected:
{
"result" : "Not Matched"
}
Can anyone please suggest me help to work it as usual.
You can transform it in three steps:
move values to keys
find matched key
add "Not match" result if matched key was not found
(can't imagine why it doesn't work with values)
Smth like:
[
{
"operation": "shift",
"spec": {
"*": "&.#0"
}
},
{
"operation": "shift",
"spec": {
"a": {
"*": {
"#(2,b.&)": {
"#Matched": "result"
}
}
}
}
},
{
"operation": "default",
"spec": {
"result": "Not Matched"
}
}
]