json 震动变换:如何将值从列表中转移到每个Map

iaqfqrcu  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(126)

我一直在研究jolt变换找不到解决方案
我想有attributeType和attributeValue到每个attributeList和唯一的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
      }
    ]
  }
]
y53ybaqx

y53ybaqx1#

您可以使用以下转换

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

网站http://jolt-demo.appspot.com/上的*demo为:

相关问题