Search code examples
arraysjsondrop-down-menutransformationjolt

Jolt transformation: how to shift values to each map from list


I have been working on jolt transformation unable to find solution

I would like to have attributeType and attributeValue to each attributeList and unique modelId and they key names should be case sensitive in expected output

Input

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

Expected output

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

Solution

  • You can use the following transformation

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

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here