json - Need jolt spec for dynamic colum - Stack Overflow

admin2025-04-17  3

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"
    }
  ]
}

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"
    }
  ]
}
Share edited Feb 4 at 7:08 Optimizer asked Jan 31 at 13:08 OptimizerOptimizer 2711 gold badge2 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 :

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].&1.codes[]" //increment the indexes of &4[&3].&2[&1] by 1 respectively as it stays 1 more level deeper
              }
            }
          },
          "*": "&2[&1].&"
        }
      }
    }
  }
]
转载请注明原文地址:http://anycun.com/QandA/1744860962a88649.html