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

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

Jolt Transformation: how to shift values to each map

问题

[
  {
    "modelId": "Phone-A",
    "attributes": [
      {
        "attributeType": "memory",
        "attributeValue": "8"
      },
      {
        "attributeType": "catagory",
        "attributeValue": "phone"
      }
    ]
  },
  {
    "modelId": "Tab-B",
    "attributes": [
      {
        "attributeType": "memory",
        "attributeValue": "16"
      },
      {
        "attributeType": "catagory",
        "attributeValue": "tablet"
      }
    ]
  }
]
英文:

I have been working on jolt transformation unable to find solution.

I would like to have attributeType and attributeValue to each attributeList and modelId unique

Input

[
  {
    "modelId": "Phone-A",
    "attributeType": "memory",
    "attributeValue": "8"
  },
  {
    "modelId": "Phone-A",
    "attributeType": "catagory",
    "attributeValue": "phone"
  },
  {
    "modelId": "Tab-B",
    "attributeType": "memory",
    "attributeValue": "16"
  },
  {
    "modelId": "Tab-B",
    "attributeType": "catagory",
    "attributeValue": "tablet"
  }
]

Expected output:

[
  {
    "modelId": "Phone-A",
    "attributes": [
      {
        "attributeType": "memory",
        "attributeValue": "8"
      },
      {
        "attributeType": "catagory",
        "attributeValue": "phone"
      }
    ]
  },
  {
    "modelId": "Tab-B",
    "attributes": [
      {
        "attributeType": "memory",
        "attributeValue": "16"
      },
      {
        "attributeType": "catagory",
        "attributeValue": "tablet"
      }
    ]
  }
]

答案1

得分: 0

您可以使用以下转换

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "modelId": "@1,modelId.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "modelId": "@1,modelId.&",
        "@": ""
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "modelId": "ONE"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

网站http://jolt-demo.appspot.com/上的演示如下:

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

英文:

You can use the following transformation

[
  { // group by "modelId" values
    "operation": "shift",
    "spec": {
      "*": {
        "modelId": "@1,modelId.&",
        "*": "@1,modelId.attributes[#2].&"
      }
    }
  },
  { // get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "modelId": "@1,modelId.&",
        "@": ""
      }
    }
  },
  { // pick only one from repeating components of the "modelId" array
    "operation": "cardinality",
    "spec": {
      "*": {
        "modelId": "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日 17:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505495.html
匿名

发表评论

匿名网友

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

确定