Search code examples
jsonjolt

Need jolt spec for dynamic colum


Need a jolt spec for below input json to produce below output json. Under "inventoryTypes" container I need a dynamic container called "codes" and each item (ex-KDXN,KLON) under "inventoryTypes.codes" should mark as key code and the value should be the item like (ex-KDXN,KLON) dynamically in output.

input JSON

{
  "pricePlans": [
    {
      "extraPersonCharge": "250",
      "amountRules": [
        {
          "type": "AmountRule",
          "period": {
            "start": "2025-04-01",
            "end": "2025-04-05",
            "dow": "-TWTFS-"
          },
          "inventoryTypes": {
            "KDXN": [
              {
                "computationType": "Flat",
                "value": "21000"
              },
              {
                "computationType": "Flat",
                "value": "200"
              }
            ],
            "KLON": [
              {
                "computationType": "Flat",
                "value": "22000"
              }
            ]
          }
        }
      ],
      "chargeMethod": "PerProduct",
      "chargeFrequency": "PerNight",
      "productCode": "SR"
    }
  ]
}

Output JSON

{
  "pricePlans": [
    {
      "amountRules": [
        {
          "type": "AmountRule",
          "period": {
            "start": "2025-04-01",
            "end": "2025-04-05",
            "dow": "-TWTFS-"
          },
          "inventoryTypes": {
            "codes": [
              {
                "code": "KDXN",
                "computationType": "Flat",
                "value": "21000"
              },
              {
                "code": "KDXN",
                "computationType": "Flat",
                "value": "200"
              },
              {
                "code": "KLON",
                "computationType": "Flat",
                "value": "22000"
              }
            ]
          }
        }
      ],
      "chargeMethod": "PerProduct",
      "chargeFrequency": "PerNight",
      "productCode": "SR"
    }
  ]
}

Solution

  • You can use the following shift transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "amountRules": {
                "*": {
                  "*": "&4[&3].&2[&1].&", //the elements other than "inventoryTypes"
                  "inventoryTypes": {
                    "*": {
                      "*": {
                        "$1": "&7[&6].&5[&4].&3.codes[#3].code", //the desired attribute, namely  "code" is added
                        "*": "&7[&6].&5[&4].&3.codes[#3].&"//combine the attributes under common objects of the "codes" array by indexes through use of [#3]
                      }
                    }
                  }
                }
              },
              "*": "&2[&1].&"//the elements other than "amountRules"
            }
          }
        }
      }
    ]
    

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

    Jolt Transform Demo

    Edit (due to the lately updated case) : You can use the below one for the lately edited case

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "amountRules": {
                "*": {
                  "*": "&4[&3].&2[&1].&",
                  "inventoryTypes": {
                    "*": {
                      "*": {
                        "$1": "&7[&6].&5[&4].&3.&1_&2.code", // two levels separation occurs through use of &1_&2
                        "*": "&7[&6].&5[&4].&3.&1_&2.&"
                      }
                    }
                  }
                }
              },
              "*": "&2[&1].&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "amountRules": {
                "*": {
                  "*": "&4[&3].&2[&1].&",
                  "inventoryTypes": {
                    "*": "&5[&4].&3[&2].codes[]" //increment the indexes of &4[&3].&2[&1] by 1 respectively as it stays 1 more level deeper
                  }
                }
              },
              "*": "&2[&1].&"
            }
          }
        }
      }
    ]