json 基于属性在Jolt中添加新的序列列(计数器)

vql8enpb  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(111)

基于属性在Jolt中添加新的序列列(计数器)
我正在编写一个Jolt来基于ProductId项添加一个Id列(它应该是增量的)。一个产品可以有多个子产品,有时只有1个子产品。对于特定的productId,我的SubProductID列应该始终以1开始。

电流输入

[
  {
    "ProductId": "123",
    "ProductName": "ABC",
    "SubProductName": "SUB1"
  },
  {
    "ProductId": "123",
    "ProductName": "ABC",
    "SubProductName": "SUB2"
  },
  {
    "ProductId": "888",
    "ProductName": "DEF",
    "SubProductName": "DEF1"
  },
  {
    "ProductId": "999",
    "ProductName": "XYZ",
    "SubProductName": "RET"
  }
]

字符串

预期输出

[
  {
    "ProductId": "123",
    "ProductName": "ABC",
    "SubProductName": "SUB1",
    "SubProductIDSEQ": "1"
  },
  {
    "ProductId": "123",
    "ProductName": "ABC",
    "SubProductName": "SUB2",
    "SubProductIDSEQ": "2"
  },
  {
    "ProductId": "888",
    "ProductName": "DEF",
    "SubProductName": "DEF1",
    "SubProductIDSEQ": "1"
  },
  {
    "ProductId": "999",
    "ProductName": "XYZ",
    "SubProductName": "RET",
    "SubProductIDSEQ": "1"
  }
]


任何帮助都非常感谢

8wigbo56

8wigbo561#

正如我们所知,数组的索引从零开始,因此我们将在将它们按ProductId值分组后,通过使用*intSum*函数将它们递增1
您可以使用以下转换来获得所需的结果:

[
  { // group the objects by "ProductId" values
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@1,ProductId[]"
      }
    }
  },
  { // make all indexes starting from zero per each "ProductId" value
    // while generating the new attribute "SubProductIDSEQ"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2.&1",
          "$": "&2.&1.SubProductIDSEQ"
        }
      }
    }
  },
  { // increment the values of "SubProductIDSEQ" to make it starting from 1 per each group
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "SubProductIDSEQ": "=intSum(1,@(1,&))"
        }
      }
    }
  },
  { // back to the original form of the JSON
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

字符串
网站http://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据

相关问题