Mahesh Gopalan
Mahesh Gopalan

Reputation: 25

Jolt Spec transformation with nested Array structure - follow up

I have a json like below;

{
  "Product": [
    {
      "id": 123,
      "name": "foo",
      "ProductCharge": [
        {
          "aboveAmount": 0,
          "chargeType": "ABC",
          "chargeValueType": "PERCENT",
          "frequency": 1,
          "feeRate": [
            {
              "bandNumber": 1,
              "rate": 0.6
            },
            {
              "bandNumber": 2,
              "rate": 1.7
            }
          ]
        },
        {
          "aboveAmount": 0,
          "chargeType": "DEF",
          "chargeValueType": "DECIMAL",
          "frequency": 2,
          "feeRate": [
            {
              "bandNumber": 1,
              "rate": 0.75
            },
            {
              "bandNumber": 2,
              "rate": -0.55
            }
          ]
        }
      ]
    }
  ]
}

which needs to be transformed to below structure. The ProductCharge object should repeat for each feeRate array with the remaining fields from ProductCharge;

{
  "Product" : [ {
    "id" : 123,
    "name" : "foo",
    "ProductCharge" : [ {
      "aboveAmount" : 0,
      "chargeType" : "ABC",
      "chargeValueType" : "PERCENT",
      "frequency" : 1,
      "bandNumber" : 1,
      "rate" : 0.6
    }, {
      "aboveAmount" : 0,
      "chargeType" : "ABC",
      "chargeValueType" : "PERCENT",
      "frequency" : 1,
      "bandNumber" : 2,
      "rate" : 1.7
    }, {
      "aboveAmount" : 0,
      "chargeType" : "DEF",
      "chargeValueType" : "DECIMAL",
      "frequency" : 2,
      "bandNumber" : 1,
      "rate" : 0.75
    }, {
      "aboveAmount" : 0,
      "chargeType" : "DEF",
      "chargeValueType" : "DECIMAL",
      "frequency" : 2,
      "bandNumber" : 2,
      "rate" : -0.55
    } ]
  } ]
}

The below jolt spec does the transformation, but values of aboveAmount, chargeType, chargeValueType, frequency are picked from the first ProductCharge object instead from each one;

[
  {
    "operation": "shift",
    "spec": {
      "Product": {
        "*": {
          "*": "&2.&",
          "ProductCharge": {
            "0": { //in order to bring only once among duplicated values - this is not right!!! 
              "*": "&4.&2.Others.&",
              "feeRate": {
                "*": {
                  "*": "&6.&4.&2.&3_&1.&"
                }
              }
            },
            "*": {
              "feeRate": {
                "*": {
                  "*": "&6.&4.&2.&3_&1.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Product": {
        "*": "&1[0].&",
        "ProductCharge": {
          "feeRate": {
            "*": {
              "@": "&4[0].&3.[#2]",
              "@2,Others": { "*": "&5[0].&4.[#3].&" }
            }
          }
        }
      }
    }
  }
]

Need to change the values of ProductCharge too. This is the earlier solution provided; Jolt Spec transformation with nested Array structure

Upvotes: 0

Views: 33

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can prefer using the following one this time :

[
  {
    "operation": "shift",
    "spec": {
      "Product": {
        "*": {
          "*": "&2.&",
          "ProductCharge": {
            "*": {
              "*": "&4.&2[&1].Others.&",
              "feeRate": {
                "*": {
                  "*": "&6.&4[&3].&2[&1].&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Product": {
        "*": "&1.&",
        "ProductCharge": {
          "*": {
            "feeRate": {
              "*": {
                "@2,Others": { "*": "&6.&5.&4_&1.&" }, // go two levels up the tree to grab the values of the "Others" array
                "*": "&5.&4.&3_&1.&"
              }
            }
          }
        }
      }
    }
  },
  {//get rid of the inner object keys
    "operation": "shift",
    "spec": {
      "Product": {
        "*": "&1.&",
        "ProductCharge": {
          "*": {
            "@": "&3.&2[]"
          }
        }
      }
    }
  }
]

the demo on the site Jolt Transform Demo Using v0.1.1 is :

enter image description here

Upvotes: 1

Related Questions