如何在Jolt变换的if条件中添加for循环

问题描述 投票:0回答:1

我有以下输入 JSON,我想在 if 条件内添加 for 循环

输入

[
  {
    "sports": {
      "feeType": [
        {
          "sport_fee_type": "Discounted fee",
          "sport_fee_total": 44730.77,
          "fee_details": [
            {
              "total": 1117.3,
              "sports_catagory": "Cricket"
            },
            {
              "total": 6187.79,
              "sports_catagory": "Football"
            }
          ]
        },
        {
          "sport_fee_type": "Recurring fee",
          "sport_fee_total": 44730.77,
          "fee_details": [
            {
              "total": 1117.3,
              "sports_catagory": "Cricket"
            },
            {
              "total": 6187.79,
              "sports_catagory": "Football"
            }
          ]
        },
        {
          "sport_fee_type": "Non Recurring fee",
          "sport_fee_total": 44730.77,
          "fee_details": [
            {
              "total": 1117.3,
              "sports_catagory": "Cricket"
            },
            {
              "total": 6187.79,
              "sports_catagory": "Football"
            }
          ]
        }
      ]
    }
  }
]

规格

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@sports": {
          "@feeType": {
            "*": {
              "@sport_fee_type": {
                "Discounted fee": {
                  "@fee_details": {
                    "*": {
                      "@total": "billing.feeSummary[#4].sportsCategories.amount",
                      // Expecting Sports catagory in upper case
                      "@sports_catagory": "billing.feeSummary[#4].sportsCategories.sportType"
                    }
                  },
                  "#DISCOUNTED": "billing.feeSummary[#4].type"
                },
                "Non Recurring fee": {
                  "@fee_details": {
                    "*": {
                      "@total": "billing.feeSummary[#4].sportsCategories.amount",
                      // Expecting Sports catagory in upper case
                      "@sports_catagory": "billing.feeSummary[#4].sportsCategories.sportType"
                    }
                  },
                  "#NON_RECURRING": "billing.feeSummary[#4].type"
                },
                "Recurring fee": {
                  "@fee_details": {
                    "*": {
                      "@total": "billing.feeSummary[#4].sportsCategories.amount",
                      // Expecting Sports catagory in upper case
                      "@sports_catagory": "billing.feeSummary[#4].sportsCategories.sportType"
                    }
                  },
                  "#RECURRING": "billing.feeSummary[#4].type"
                }
              }
            }
          }
        }
      }
    }
  }]

预期产出

{
  "billing": {

    "feeSummary": [
      {
        "feeType": "RECURRING",
        "sportsCategories": [
          {
            "sportType": "CRICKET",
            "amount": 1117.3
          },
          {
            "sportType": "FOOTBALL",
            "amount": 6187.79
          }
        ]
      },
      {
        "feeType": "NON_RECURRING",
        "sportsCategories": [
          {
            "sportType": "CRICKET",
            "amount": 1117.3
          },
          {
            "sportType": "FOOTBALL",
            "amount": 6187.79
          }
        ]
      },
      {
        "feeType": "DISCOUNTED",
        "sportsCategories": [
          {
            "sportType": "CRICKET",
            "amount": 1117.3
          },
          {
            "sportType": "FOOTBALL",
            "amount": 6187.79
          }
        ]
      }
    ]
  }
}
json transform jolt
1个回答
0
投票

您可以考虑使用数组节点通过 [&5][&1] 标识符循环遍历它们,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@sports.feeType": {
          "*": {
            "sport_fee_type": {
              "Discounted fee": {
                "@2,fee_details": {
                  "*": {
                    "total": "billing.feeSummary[&5].sportsCategories[&1].amount",
                    "sports_catagory": "billing.feeSummary[&5].sportsCategories[&1].sportType"
                  }
                },
                "#DISCOUNTED": "billing.feeSummary[#2].feeType"
              },
              "Non Recurring fee": {
                "@2,fee_details": {
                  "*": {
                    "total": "billing.feeSummary[&5].sportsCategories[&1].amount",
                    "sports_catagory": "billing.feeSummary[&5].sportsCategories[&1].sportType"
                  }
                },
                "#NON_RECURRING": "billing.feeSummary[#4].feeType"
              },
              "Recurring fee": {
                "@2,fee_details": {
                  "*": {
                    "total": "billing.feeSummary[&5].sportsCategories[&1].amount",
                    "sports_catagory": "billing.feeSummary[&5].sportsCategories[&1].sportType"
                  }
                },
                "#RECURRING": "billing.feeSummary[#4].feeType"
              }
            }
          }
        }
      }
    }
  },
  { // convert the values of sportType attributes to uppercase 
    "operation": "modify-overwrite-beta",
    "spec": {
      "billing": {
        "feeSummary": {
          "*": {
            "sportsCategories": {
              "*": {
                "sportType": "=toUpper"
              }
            }
          }
        }
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.