英文:
Jolt transformation: how to shift values to each map from list
问题
我一直在进行 jolt 转换工作,无法找到解决方案。
我想要在每个 attributeList 中添加 attributeType 和 attributeValue,并且唯一的 modelId,键名应该在预期输出中区分大小写。
输入
[
{
"MODELNUMBER": "A",
"ATTRIBUTETYPE": "Capability",
"ATTRIBUTEID": "0001",
"ATTRIBUTENAME": "hd",
"ATTRIBUTEVALUE": "false",
"RNK": 1,
"batchNumber": 1
},
{
"MODELNUMBER": "A",
"ATTRIBUTETYPE": "Capacity",
"ATTRIBUTEID": "0002",
"ATTRIBUTENAME": "capa",
"ATTRIBUTEVALUE": "100",
"RNK": 2,
"batchNumber": 1
},
{
"MODELNUMBER": "B",
"ATTRIBUTETYPE": "Capability",
"ATTRIBUTEID": "cat-0002",
"ATTRIBUTENAME": "capable",
"ATTRIBUTEVALUE": "true",
"RNK": 1,
"batchNumber": 1
}
]
预期输出
[
{
"inventoryModelId": "A",
"attributes": [
{
"attributeType": "Capability",
"attributeId": "0001",
"attributeName": "hd",
"attributeValue": "false",
"rnk": 1,
"batchNumber": 1
},
{
"attributeType": "Capacity",
"attributeId": "0002",
"attributeName": "capa",
"attributeValue": "100",
"rnk": 2,
"batchNumber": 1
}
]
},
{
"inventoryModelId": "B",
"attributes": [
{
"attributeType": "Capability",
"attributeId": "cat-0002",
"attributeName": "capable",
"attributeValue": "true",
"rnk": 1,
"batchNumber": 1
}
]
}
]
英文:
I have been working on jolt transformation unable to find solution
I would like to have attributeType and attributeValue to each attributeList and unique modelId and they key names should be case sensitive in expected output
Input
[
{
"MODELNUMBER": "A",
"ATTRIBUTETYPE": "Capability",
"ATTRIBUTEID": "0001",
"ATTRIBUTENAME": "hd",
"ATTRIBUTEVALUE": "false",
"RNK": 1,
"batchNumber": 1
},
{
"MODELNUMBER": "A",
"ATTRIBUTETYPE": "Capacity",
"ATTRIBUTEID": "0002",
"ATTRIBUTENAME": "capa",
"ATTRIBUTEVALUE": "100",
"RNK": 2,
"batchNumber": 1
},
{
"MODELNUMBER": "B",
"ATTRIBUTETYPE": "Capability",
"ATTRIBUTEID": "cat-0002",
"ATTRIBUTENAME": "capable",
"ATTRIBUTEVALUE": "true",
"RNK": 1,
"batchNumber": 1
}
]
Expected output
[
{
"inventoryModelId": "A",
"attributes": [
{
"attributeType": "Capability",
"attributeId": "0001",
"attributeName": "hd",
"attributeValue": "false",
"rnk": 1,
"batchNumber": 1
},
{
"attributeType": "Capacity",
"attributeId": "0002",
"attributeName": "capa",
"attributeValue": "100",
"rnk": 2,
"batchNumber": 1
}
]
},
{
"inventoryModelId": "B",
"attributes": [
{
"attributeType": "Capability",
"attributeId": "cat-0002",
"attributeName": "capable",
"attributeValue": "true",
"rnk": 1,
"batchNumber": 1
}
]
}
]
答案1
得分: 0
你可以使用以下转换
[
{ // 按“MODELNUMBER”值分组
"operation": "shift",
"spec": {
"*": {
"MODELNUMBER": "@1,MODELNUMBER.inventoryModelId",
"ATTRIBUTETYPE": "@1,MODELNUMBER.attributes[#2].attributeType",
"ATTRIBUTEID": "@1,MODELNUMBER.attributes[#2].attributeId",
"ATTRIBUTENAME": "@1,MODELNUMBER.attributes[#2].attributeName",
"ATTRIBUTEVALUE": "@1,MODELNUMBER.attributes[#2].attributeValue",
"RNK": "@1,MODELNUMBER.attributes[#2].rnk",
"batchNumber": "@1,MODELNUMBER.attributes[#2].&"
}
}
},
{ // 去掉对象键
"operation": "shift",
"spec": {
"*": {
"*": "@1,inventoryModelId.&",
"@": ""
}
}
},
{ // 从“modelId”数组的重复组件中仅选择一个
"operation": "cardinality",
"spec": {
"*": {
"inventoryModelId": "ONE"
}
}
},
{ // 去掉最近生成的null值
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
在网站 http://jolt-demo.appspot.com/ 上的 演示 是:
英文:
You can use the following transformation
[
{ // group by "MODELNUMBER" values
"operation": "shift",
"spec": {
"*": {
"MODELNUMBER": "@1,MODELNUMBER.inventoryModelId",
"ATTRIBUTETYPE": "@1,MODELNUMBER.attributes[#2].attributeType",
"ATTRIBUTEID": "@1,MODELNUMBER.attributes[#2].attributeId",
"ATTRIBUTENAME": "@1,MODELNUMBER.attributes[#2].attributeName",
"ATTRIBUTEVALUE": "@1,MODELNUMBER.attributes[#2].attributeValue",
"RNK": "@1,MODELNUMBER.attributes[#2].rnk",
"batchNumber": "@1,MODELNUMBER.attributes[#2].&"
}
}
},
{ // get rid of the object keys
"operation": "shift",
"spec": {
"*": {
"*": "@1,inventoryModelId.&",
"@": ""
}
}
},
{ // pick only one from repeating components of the "modelId" array
"operation": "cardinality",
"spec": {
"*": {
"inventoryModelId": "ONE"
}
}
},
{ // get rid of the lately generated null values
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论