将平面JSON转换为包含数组的嵌套JSON

bzzcjhmw  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(127)

我正面临一个问题,使用jolt转换将平面JSON转换为嵌套JSON。我对jolt转换非常陌生。输入和输出细节如下所示。
我的输入

[
  {
    "ProposalId": "1234",
    "ReplStrategy": "External",
    "CreatedDate": "2022-10-26",
    "ValidFromDate": "2022-10-26",
    "DeliveryDate": "2022-10-29",
    "InventLocationIdFrom": "10",
    "InventLocationIdTo": "12",
    "RetailVariantId": "123457",
    "Qty": 12
  },
  {
    "ProposalId": "1234",
    "ReplStrategy": "External",
    "CreatedDate": "2022-10-26",
    "ValidFromDate": "2022-10-26",
    "DeliveryDate": "2022-10-29",
    "InventLocationIdFrom": "10",
    "InventLocationIdTo": "12",
    "RetailVariantId": "123458",
    "Qty": 16
  },
  {
    "ProposalId": "1235",
    "ReplStrategy": "External",
    "CreatedDate": "2022-10-26",
    "ValidFromDate": "2022-10-26",
    "DeliveryDate": "2022-10-29",
    "InventLocationIdFrom": "10",
    "InventLocationIdTo": "12",
    "RetailVariantId": "123459",
    "Qty": 12
  },
  {
    "ProposalId": "1235",
    "ReplStrategy": "External",
    "CreatedDate": "2022-10-26",
    "ValidFromDate": "2022-10-26",
    "DeliveryDate": "2022-10-29",
    "InventLocationIdFrom": "10",
    "InventLocationIdTo": "12",
    "RetailVariantId": "123460",
    "Qty": 16
  },
  {
    "ProposalId": "1235",
    "ReplStrategy": "External",
    "CreatedDate": "2022-10-26",
    "ValidFromDate": "2022-10-26",
    "DeliveryDate": "2022-10-29",
    "InventLocationIdFrom": "10",
    "InventLocationIdTo": "12",
    "RetailVariantId": "123461",
    "Qty": 16
  }
]

预期产量

{
    "Proposal": [
        {
            "ProposalId": "1234",
            "ReplStrategy": "External",
            "CreatedDate": "2022-10-26",
            "ValidFromDate": "2022-10-26",
            "DeliveryDate": "2022-10-29",
            "InventLocationIdFrom": "10",
            "InventLocationIdTo": "12",
            "RetailVariant": [
                {
                    "RetailVariantId": "123456",
                    "Qty": 15,
                },
                {
                    "RetailVariantId": "123457",
                    "Qty": 12,
                }
            ]
        },
        {
            "ProposalId": "1235",
            "ReplStrategy": "TwoPhased",
            "CreatedDate": "2022-10-26",
            "ValidFromDate": "2022-10-26",
            "DeliveryDate": "2022-10-29",
            "InventLocationIdFrom": "10",
            "InventLocationIdTo": "12",
            "RetailVariant": [
                {
                    "RetailVariantId": "123458",
                    "Qty": 13,
                },
                {
                    "RetailVariantId": "123459",
                    "Qty": 11,
                }
            ]
        }
    ]
}

我编写了jolt spec,但没有得到所需的输出
震动i使用

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ProposalId": "@(1,ProposalId).Proposal.&",
        "ReplStrategy": "@(1,ProposalId).Proposal.&",
        "CreatedDate": "@(1,ProposalId).Proposal.&",
        "ValidFromDate": "@(1,ProposalId).Proposal.&",
        "DeliveryDate": "@(1,ProposalId).Proposal.&",
        "InventLocationIdFrom": "@(1,ProposalId).Proposal.&",
        "InventLocationIdTo": "@(1,ProposalId).Proposal.&",
        "*": "@(1,ProposalId).Proposal.RetailVariant[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "ProposalId": "ONE",
          "ReplStrategy": "ONE",
          "CreatedDate": "ONE",
          "ValidFromDate": "ONE",
          "DeliveryDate": "ONE",
          "InventLocationIdFrom": "ONE",
          "InventLocationIdTo": "ONE"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

任何一个震动Maven都可以帮助我得到想要的输出吗?我想我在最后一步卡住了。

suzh9iv8

suzh9iv81#

您可以使用

[
  { // group by ProposalId values
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,ProposalId).&",
        "RetailVariantId|Qty": {
          "@": "@(2,ProposalId).RetailVariant[&2].&"
        }
      }
    }
  },
  { // nest all JSON value within Proposal array
    "operation": "shift",
    "spec": {
      "*": "Proposal[]"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": "ONE",
          "RetailVariant": "MANY"
        }
      }
    }
  },
  { // get rid of redundant nulls
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

http://jolt-demo.appspot.com/站点上的***演示***是

相关问题