0

这是一个电子商务用例,我需要消耗一天内的所有订单,生成单个订单标题,并列出订单项的汇总摘要。这意味着我需要根据共享值 (sku) 对订单项对象数组进行重复数据删除。我还需要根据相同的值对某些字段(数量、总购买价格)求和。

示例输入:

{
  "Account": {
    "Account Name": "Firefly",
    "Order": [
      {
        "OrderID": "order103",
        "Product": [
          {
            "Product Name": "Bowler Hat",
            "ProductID": 858383,
            "SKU": "0406654608",
            "Description": {
              "Colour": "Purple",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.75
            },
            "Price": 50.0,
            "Quantity": 2
          },
          {
            "Product Name": "Trilby hat",
            "ProductID": 858236,
            "SKU": "0406634348",
            "Description": {
              "Colour": "Orange",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.6
            },
            "Price": 21.67,
            "Quantity": 1
          }
        ]
      },
      {
        "OrderID": "order104",
        "Product": [
          {
            "Product Name": "Bowler Hat",
            "ProductID": 858383,
            "SKU": "040657863",
            "Description": {
              "Colour": "Purple",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.75
            },
            "Price": 100,
            "Quantity": 4
          },
          {
            "ProductID": 345664,
            "SKU": "0406654603",
            "Product Name": "Cloak",
            "Description": {
              "Colour": "Black",
              "Width": 30,
              "Height": 20,
              "Depth": 210,
              "Weight": 2
            },
            "Price": 107.99,
            "Quantity": 1
          }
        ]
      }
    ]
  }
}

期望的输出:

{
  "header": "Some Header Fields...",
  "line items": [
    {
      "Product Name": "Bowler Hat",
      "ProductID": 858383,
      "SKU": "0406654608",
      "Description": {
        "Colour": "Purple",
        "Width": 300,
        "Height": 200,
        "Depth": 210,
        "Weight": 0.75
      },
      "Price": 150,
      "Quantity": 6
    },
    {
      "Product Name": "Trilby hat",
      "ProductID": 858236,
      "SKU": "0406634348",
      "Description": {
        "Colour": "Orange",
        "Width": 300,
        "Height": 200,
        "Depth": 210,
        "Weight": 0.6
      },
      "Price": 21.67,
      "Quantity": 1
    },
    {
      "ProductID": 345664,
      "SKU": "0406654603",
      "Product Name": "Cloak",
      "Description": {
        "Colour": "Black",
        "Width": 30,
        "Height": 20,
        "Depth": 210,
        "Weight": 2
      },
      "Price": 107.99,
      "Quantity": 1
    }
  ]
}

我知道我可以使用 $distinct() 获得一组 skus,但我不知道如何生成完全不同的对象并保留其值。

我认为我不能应用合并,因为对象的某些部分(数量)不是唯一的,而且我不知道如何仅在对象本身的某些字段上进行合并。

最后,我在一个没有代码片段的平台上工作,所以我不能调用 javascript。这在纯 JSONata 中可能吗?

4

1 回答 1

1

找到分组文档。我可以使用 $lineItems := Account.Order.Product{ SKU:{ "price": $sum(Price), "qty":$sum(Quantity), "sku":$distinct(SKU) 按 sku 分组}}

这会生成一个节点,其中每个键都是 SKU,因此我映射了 "output_line_items": $lineItems.* 它为我提供了每个 sku 字段下的条目。

请注意,在我的示例中,SKU 实际上并不匹配跨产品,ProductID 会是一个更好的示例。但是 SKU 用于我的特定用例。

于 2020-03-04T14:52:54.450 回答