Search code examples
arraysjsonlisttransformjolt

Jolt Transformation: how to shift values to each map


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"
      }
    ]
  }
]

Solution

  • 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

    enter image description here