jsonjolt

Jolt Transformation - Group Similar Values into new Keys


This is my input data:

[
  {
    "UniqueID": 1,
    "Group": "alpha",
    "Type": "t/1"
  },
  {
    "UniqueID": 2,
    "Group": "alpha",
    "Type": "t/14"
  },
  {
    "UniqueID": 3,
    "Group": "beta",
    "Type": "t/11"
  },
  {
    "UniqueID": 4,
    "Group": "charlie",
    "Type": "t/10"
  },
  {
    "UniqueID": 5,
    "Group": "beta",
    "Type": "t/7"
  }
]

and I am hoping to get:

{
  "alpha": ["t/1", "t/14"],
  "beta": ["t/11", "t/7"],
  "charlie": ["t/10"]
}

This is what I was trying to do, but wasn't getting what I had hoped.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Group": "&2.Type"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": "ONE"
    }
  }
]

Is anyone able to offer some guidance, as I just cannot seem to get it to work. Thank you


Solution

  • You can directly match both sides by using @ prefix such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@Type": "@Group[]"//the ending [] keeps existence of the wrapper for the             
                               //single-valued attribute(s), eg. "charlie"
          }
        }
      }
    ]