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

huangapple go评论88阅读模式
英文:

Adding new Sequence Column(Counter) in Jolt based on a Attribute

问题

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

我正在编写一个 Jolt,根据 ProductId 项目添加一个 Id 列(应该是增量的)。一个产品可以有多个子产品,有时只有一个。我的 SubProductID 列应该始终从特定的 productId 开始,始终为 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"
  }
]

非常感谢任何帮助。

英文:

Adding new Sequence Column(Counter) in Jolt based on a Attribute

I am writing a Jolt to add a Id column (it should be incremental)based on the ProductId item . A product can have multiple sub products or sometimes only 1. My SubProductID column should start always start with 1 for a particular productId.

Current Input :

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

Output Expected :

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

Any help is much appreciated

答案1

得分: 2

正如我们所知,数组的索引从零开始,所以我们将通过使用 intSum 函数将它们增加 1,在按 ProductId 值对它们进行分组后。

你可以使用以下转换来获得所需的结果:

[
  { // 按“ProductId”值对对象进行分组
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@1,ProductId[]"
      }
    }
  },
  { // 使每个“ProductId”值的所有索引从零开始
    // 同时生成新属性“SubProductIDSEQ”
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2.&1",
          "$": "&2.&1.SubProductIDSEQ"
        }
      }
    }
  },
  { // 增加“SubProductIDSEQ”的值,使其在每个组中从1开始
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "SubProductIDSEQ": "=intSum(1,@(1,&))"
        }
      }
    }
  },
  { // 回到JSON的原始形式
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

在网站 http://jolt-demo.appspot.com/ 上的 演示 是:

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

英文:

As we know, the indexes of the arrays start from zero, so we'll increment them by 1 through use of intSum function after having got them grouped by the ProductId values

you can use the following transformation in order to get the desired result :

[
  { // 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": {
      "*": {
        "*": ""
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

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

huangapple
  • 本文由 发表于 2023年7月27日 18:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定