Jolt 转换:如何将值从列表移动到每个映射中

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

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/ 上的 演示 是:

Jolt 转换:如何将值从列表移动到每个映射中

英文:

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 :

Jolt 转换:如何将值从列表移动到每个映射中

huangapple
  • 本文由 发表于 2023年6月19日 21:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507133.html
匿名

发表评论

匿名网友

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

确定