I have an array of JSON Objects that I need to transform into a new array of JSON Objects that align with my schema. I am new to Jolt and my output is not what I need.
I tried multiple ways but not able to get my desired result.
This is my Input JSON:
{
"totalApplication":{
"applicationName":"MSOffice",
"products":[
{
"productName":"Yearly",
"userList":[
{
"userId":"3544353",
"email":"[email protected]",
"userName":"Joe Hashashi",
"lastActive":"1705088660"
},
{
"userId":"3544354",
"email":"[email protected]",
"userName":"Jacob Martin",
"lastActive":"1705088661"
}
]
},
{
"productName":"Monthly",
"userList":[
{
"userId":"4545455",
"email":"[email protected]",
"userName":"Amanda Kore",
"lastActive":"1705088662"
},
{
"userId":"4545456",
"email":"[email protected]",
"userName":"Leo Biz",
"lastActive":"1705088663"
}
]
}
]
}
}
This is the Jolt Spec I tried:
[
{
"operation": "shift",
"spec": {
"totalApplication": {
"products": {
"*": {
"userList": {
"*": {
"userName": "[&3].name",
"email": "[&3].email",
"userId": "[&3].userId",
"@(2,productName)": "[&3].additionalAttribute.planName",
"lastActive": "[&3].additionalAttribute.lastActive",
"@(4,applicationName)": "[&3].additionalAttribute.applicationName"
}
}
}
}
}
}
}
]
With above mentioned specs I am getting below result:
[ {
"additionalAttribute" : {
"planName" : [ "Yearly", "Yearly" ],
"applicationName" : [ "MSOffice", "MSOffice" ],
"lastActive" : [ "1705088660", "1705088661" ]
},
"name" : [ "Joe Hashashi", "Jacob Martin" ],
"email" : [ "[email protected]", "[email protected]" ],
"userId" : [ "3544353", "3544354" ]
}, {
"additionalAttribute" : {
"planName" : [ "Monthly", "Monthly" ],
"applicationName" : [ "MSOffice", "MSOffice" ],
"lastActive" : [ "1705088662", "1705088663" ]
},
"name" : [ "Amanda Kore", "Leo Biz" ],
"email" : [ "[email protected]", "[email protected]" ],
"userId" : [ "4545455", "4545456" ]
} ]
I want this Desired Output:
[
{
"name":"Joe Hashashi",
"email":"[email protected]",
"userId":"3544353",
"additionalAttribute":{
"lastActive":"1705088660",
"productName":"Yearly",
"applicationName":"MSOffice"
}
},
{
"name":"Jacob Martin",
"email":"[email protected]",
"userId":"3544354",
"additionalAttribute":{
"lastActive":"1705088661",
"productName":"Yearly",
"applicationName":"MSOffice"
}
},
{
"name":"Amanda Kore",
"email":"[email protected]",
"userId":"4545455",
"additionalAttribute":{
"lastActive":"1705088662",
"productName":"Monthly",
"applicationName":"MSOffice"
}
},
{
"name":"Leo Biz",
"email":"[email protected]",
"userId":"4545456",
"additionalAttribute":{
"lastActive":"1705088663",
"productName":"Monthly",
"applicationName":"MSOffice"
}
}
]
Its not giving desired output. Can anyone help me with this?
2
Answers
This spec should work for you, assuming that the user IDs do not repeat among different products
Explanation:
userList
under the differentuserId
value into a dictionary. You can apply only the first operation to see the intermediate output.Bonus: If the user ids may repeat among the products we have to modify the keys of the dictionary from the previous transformation. Here is the more complicated spec with the additional initial step which fulfills this potential requirement:
You can *dynamically match the outer attributes with the attributes of the innermost objects :
(*Since, there might exist more than one attributes at those levels, eg. 2 and 4 upper levels)
the demo on the site https://jolt-demo.appspot.com/ is :