英文:
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/上的演示如下:

英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论