简体   繁体   English

Jolt 转换 - 基于 id 对元素进行分组

[英]Jolt transformation - grouping element based on id

I'm trying to group the json elements based on id, name, type like below:我正在尝试根据 id、名称、类型对 json 元素进行分组,如下所示:

input:输入:

[
  {
    "itemid": "1",
    "itemName": "coco",
    "itemType": "brg",
    "subitemId": "444",
    "subitemName": "INDICATIVE"
  },
  {
    "itemid": "1",
    "itemName": "coco",
    "itemType": "brg",
    "subitemId": "333",
    "subitemName": "BRGS"
  },
  {
    "itemid": "2",
    "itemName": "limk",
    "itemType": "cmds",
    "subitemId": "456",
    "subitemName": "NMPS"
  }
]

expected output:预期 output:

[
  {
    "itemid": "1",
    "itemName": "coco",
    "itemType": "brg",
    "subitems": [
      {
        "subitemId": "444",
        "subitemName": "INDICATIVE"
      },
      {
        "subitemId": "333",
        "subitemName": "BRGS"
      }
    ]
  },
  {
    "itemid": "2",
    "itemName": "limk",
    "itemType": "cmds",
    "subitems": [
      {
        "subitemId": "456",
        "subitemName": "NMPS"
      }
    ]
  }
]

i have tried the below spec but it is not giving expected output:我已经尝试了以下规范,但它没有给出预期的 output:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "subitemId": "subitems[&1].subitemId",
        "subitemName": "subitems[&1].subitemName",
        "*": "&"
      }
    }
  }
]

Could you please help me.请你帮助我好吗。

This spec should work for you这个规范应该适合你

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "comp_id": "=concat(@(1,itemid),'_', @(1,itemName), '_', @(1,itemType))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,comp_id).&[]"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[#2].&",
        "comp_id": null
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "itemid": "=firstElement(@(1,itemid))",
        "itemName": "=firstElement(@(1,itemName))",
        "itemType": "=firstElement(@(1,itemType))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "subitemId": {
          "*": "[&2].subitems[&].subitemId"
        },
        "subitemName": {
          "*": "[&2].subitems[&].subitemName"
        }
      }
    }
  }
]

Tested with https://jolt-demo.appspot.comhttps://jolt-demo.appspot.com测试

Try to examine it operation by operation.尝试通过操作来检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM