I have been trying multiple approaches using JOLT to convert a Json as show below:
INPUT:
[
{
"CarOwners": [
{
"Name": "john",
"car": "volvo"
},
{
"Name": "john",
"car": "Audi"
},
{
"Name": "Mike",
"car": "Audi"
}
]
}
]
OUTPUT:
[
{
"CarOwners": [
{
"Name": "john",
"car": [
"volvo",
"Audi"
]
},
{
"Name": "Mike",
"car": ["Audi"]
}
]
}
]
To put it simply, based on owner name, I need to combine the json objects & make an array of car names & populate multiple values inside the list when a person owns more than 1 car. Is it even possible using JOLT? Can you please give a hint if you know? Thank you.
I tried to use shift operation but unfortunately unable to reach anywhere near the solution.
CodePudding user response:
Hi Anand this spec will resolve your query :
[
{
"operation": "shift",
"spec": {
"*": {
"CarOwners": {
"*": {
"Name": {//If else condition to check Name is John
"john": {
"@(2,Name)": ".[#7].CarOwners.&1.Name",
"@(2,car)": "[#7].CarOwners.&1.car"
},
"Mike": {
"@(2,Name)": "[#7].CarOwners.&1.Name",
"@(2,car)": "[#7].CarOwners.&1.car"
}
}
}
}
}
}
}, {
"operation": "cardinality",
"spec": {
"*": {
"CarOwners": {
"*": {
"Name": "ONE",
"car": "MANY"
}
}
}
}
}, {
"operation": "shift",
"spec": {
"*": {
"CarOwners": {
"*": {
"*": "[#5].CarOwners.[#2].&"
}
}
}
}
}
]
CodePudding user response:
One option would be using two successive shift transformations such as
[
{
// group by Names to form individual objects
"operation": "shift",
"spec": {
"*": { // the outermost level
"*": { // for "CarOwners" array
"*": { // the indexes of the array
"car": "&2[&3].@(1,Name).&[]"
}
}
}
}
},
{
// move object labels to the location of "Name" attribute values
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"$": "&3[#2].Name",
"car": "&3[#2].&"
}
}
}
}
}
]