英文:
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/ 上的 演示 是:
英文:
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 :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论