Jolt - How to Update Key with value given with that key - json

How we can make a dynamic key based on value in Jolt?
JSON input is like
{
"Rating": 1,
"SecondaryRatings": {
"Design": 4,
"Price": 2,
"RatingDimension3": 1
}
}
And expected output should be like
{
"rating-primary-1" : 1,
"rating-Design-4" : 4,
"rating-Price-2" : 2,
"rating-RatingDimension3-1" : 1
}
Current Spec is
[
{
"operation": "shift",
"spec": {
"Rating": "rating-primary",
"SecondaryRatings": {
"*": "rating-&"
}
}
}
]

You can use two consecutive shift transformation specs such as
[
{
// determine object tags with desired prefixes
"operation": "shift",
"spec": {
"Rating": "rating-primary-.#(1,&)",
"SecondaryRatings": {
"*": "rating-&-.#(1,&)"
}
}
},
{
// then combine those keys with attribute keys inside each object
"operation": "shift",
"spec": {
"*": {
"*": "&1&"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

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

JoltTransformJson - How to get column names as values

i have JSON value as below :
{
"table": "table_name",
"op_type": "U",
"before": {
"AAAA": "1-1111",
"BBBB": "2022-08-31 03:57:01"
},
"after": {
"AAAA": "1-1111",
"BBBB": "2022-08-31 10:10:34",
"DDDD": "2023-08-31 23:59:59"
}
}
I want to add column_names field like this :
,"changed_columns": "AAAA,BBBB,DDDD"
is there a way to do this?
You can use the following specs in which the main idea is to arrange the attributes so as to generate an array with unique elements within the an array by using successive shift transformation, then combine them within a modify transformation such as
[
{
// combine common key names for each respective values for the attributes
"operation": "shift",
"spec": {
"before|after": {
"*": {
"$": "&"
}
}
}
},
{
// construct an array from those newly formed keys
"operation": "shift",
"spec": {
"*": {
"$": "changed_columns"
}
}
},
{
// make them comma-separated
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(',',#(1,&))"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Edit : If your aim is to keep newly generated attribute along with the existing ones, then you can prefer using the following spec
[
{
"operation": "shift",
"spec": {
"*": "&", //else case
"before|after": {
"*": {
"$": "cc.&",
"#": "&2.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"cc": {
"*": {
"$": "changed_columns"
}
},
"*": "&" //else case
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"changed_columns": "=join(',',#(1,&))"
}
}
]

Jolt transform array of objects to key-value

Problems
Hi, I'm using Jolt to transform e-commerce products data. For e.g:
[
{
"objectID": 1,
"string_facet": [
{
"facet_name": "eco_collection",
"facet_value": "No"
},
{
"facet_name": "performance_fabric",
"facet_value": "No"
}
]
},
{
"objectID": 2,
"string_facet": [
{
"facet_name": "activity",
"facet_value": [
"Hiking"
]
}
]
}
]
And my desired output is:
[
{
"objectID": 1,
"string_facet": {
"eco_collection": "No",
"performance_fabric": "No"
}
},
{
"objectID": 2,
"string_facet": {
"activity": [
"Hiking"
]
}
}
]
What I have done so far
I have tried this spec, but it isn't what I need:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"string_facet": {
"*": {
"#facet_value": "[&1].string_facet.#facet_name"
}
}
}
}
}
]
I'm looking for an explanation in addition to solution.
Any help would be much appreciated!
You principally need "#facet_value": "#facet_name" match, start with it, then add
&2 to represent going two levels up and grabbing the key name's value(string_facet)
&3 to represent going three levels up and grabbing the indices of the main array to accumulate each attributes nested within individual object of their own.
Then, add an extra shift transformation spec to get rid of the integer key names such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"string_facet": {
"*": {
"#facet_value": "&3.&2.#facet_name"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

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

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

Json transformation from key value to nested array

I was trying to have a key value pair mapped to an array, distinguishing each value as a type using jolt transform spec
Input json
{
"testurl": "someurl",
"website": "someurl2"
}
tried this spec
[
{
"operation": "shift",
"spec": {
"testurl": "Urls[].testurl",
"website": "Urls[].website"
}
},
{
"operation": "shift",
"spec": {
"Urls": {
"*": {
"$": "Urls[&1].val"
}
}
}
}
]
Expected result is like this
{
"Urls": [{
"url": "someurl",
"val": "testurl"
}, {
"url": "someurl2",
"val": "website"
}]
}
Yes you can turn a set of Json key,value pair into an array.
It requires 2 shifts to be safe.
1st shift isolates all the properties you want to turn into an array.
2nd shift is able to use a "*" to then match all those items and place them an array.
Spec
[
{
"operation": "shift",
"spec": {
"testurl": "temp.testurl",
"website": "temp.website"
}
},
{
"operation": "shift",
"spec": {
"temp": {
"*": {
"$": "Urls[#2].val",
"#": "Urls[#2].url"
}
}
}
}
]