I am trying to generate the JSON output based on JSON input using JOLT. Output should contain flatten array of objects for storing in db. Each object should contain following fields:
It can be seen from expected output that count of objects is equal to count of subscriptions.subscription[].options[].products[]
Input JSON :
{
"subscriptions": {
"subscription": [{
"name": "TA Generated subsr1",
"specification": "Mobile Postpaid",
"products": [{
"specification": "rateplan",
"code": "CODE1"
}, {
"specification": "addon",
"code": "P2PCF"
}, {
"specification": "addon",
"code": "AANSK"
}
],
"options": [{
"category": "Installment Plan",
"products": [{
"specification": "addon",
"code": "TA_CODE2"
}, {
"specification": "addon",
"code": "TA_CODE3"
}
],
"mandatory": "false",
"multiple": "false"
}, {
"category": "Internet",
"products": [{
"specification": "addon",
"code": "TA_CODE4"
}
],
"mandatory": "false",
"multiple": "false"
}
]
},
{
"name": "TA Generated subsr2",
"specification": "Mobile Postpaid",
"products": [{
"specification": "rateplan",
"code": "CODE11"
}, {
"specification": "addon",
"code": "P2PCF"
}, {
"specification": "addon",
"code": "AANSK"
}
],
"options": [{
"category": "Internet",
"products": [{
"specification": "addon",
"code": "TA_CODE41"
}
],
"mandatory": "false",
"multiple": "false"
}
]
}
]
}
}
Expected Output JSON:
[
{
"rateplan": "CODE1",
"code": "TA_CODE2",
"mandatory": "false",
"multiple": "false"
},
{
"rateplan": "CODE1",
"code": "TA_CODE3",
"mandatory": "false",
"multiple": "false"
},
{
"rateplan": "CODE11",
"code": "TA_CODE41",
"mandatory": "false",
"multiple": "false"
}
]
My jolt Spec:
[
{
"operation": "shift",
"spec": {
"subscriptions": {
"subscription": {
"*": {
"products": {
"*": {
"specification": {
"rateplan": {
"@(2,code)": "o[].rateplan"
}
}
}
},
"options": {
"*": {
"products": {
"*": {
"@(code)": "o[&1].code",
"@(2,mandatory)": "o[&1].mandatory",
"@(2,multiple)": "o[&1].multiple"
}
}
}
}
}
}
}
}
}
]
Thanks in advance!
We might concentrically evaluate with respect to the indexes of the outermost array, namely "subscription", as the common factor. To apply that, we can qualify the identifiers by &7 and &5 respectively such as
[
{
"operation": "shift",
"spec": {
"@subscriptions.subscription": {
"*": {
"options": {
"*": {
"products": {
"*": {
"@4,products": { // loop through the OUTER "products" array
"*": {
"@code": "&7_&3.c_s.@specification" // "match" code vs. "specification"
}
},
"@2,category": "&5_&1.category",
"code": "&5_&1.product",
"@2,mandatory": "&5_&1.mandatory",
"@2,multiple": "&5_&1.multiple"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"c_s": { "rateplan": "[#3].&" }, // filter by the key "rateplan"
"*": { "@": "[#3].&" } // other than the previously grouped attributes
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :