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:
- rateplan = subscriptions.subscription[].products[specification=rateplan].code
- code = subscriptions.subscription[].options[].products[].code
- mandatory = subscriptions.subscription[].options[].mandatory
- multiple = subscriptions.subscription[].options[].multiple
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!
2
Answers
Thanks for the help Barbaros Özhan, proposed solution is works. But there is small issue in it - in case of several option objects result is incorrect.
Example below:
I've made small amendments(additional index was added) and final spec is below:
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
the demo on the site http://jolt-demo.appspot.com/ is :