skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    [{
            "rateplan": ["CODE1", "CODE1"],
            "category": ["Installment Plan", "Internet"],
            "isMandatory": ["false", "false"],
            "isMultiple": ["false", "false"],
            "code": ["TA_CODE2", "TA_CODE3"]
        }
    ]
    

    I've made small amendments(additional index was added) and final spec is below:

    [
      {
        "operation": "shift",
        "spec": {
          "@subscriptions.subscription": {
            "*": {
              "options": {
                "*": {
                  "products": {
                    "*": {
                      "@4,products": { // loop through the OUTER "products" array
                        "*": {
                          "@code": "&7_&5_&3.c_s.@specification" // "match" code vs. "specification"
                        }
                      },
                      "@2,category": "&5_&3_&1.category",
                      "code": "&5_&3_&1.code",
                      "@2,mandatory": "&5_&3_&1.mandatory",
                      "@2,multiple": "&5_&3_&1.multiple"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "c_s": { "rateplan": "[#3].&" }, // filter by the key "rateplan"
            "*": { "@": "[#3].&" } // other than the previously grouped attributes
          }
        }
      }
    ]
    

  2. 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 :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search